javascript - Append a passed function parameter to $scope inside a controller -
i have ng-click="mymethod(parameter)"
inside <button>
element. want use parameter
, in case, string inside controller can operations $scope.parameter.subproperties
on it. in fact parameter alias name existing $scope.object.
so aim reuse mymethod()
in different places ng-click
's in <button>
's , perform same functionality on corresponding $scope
objects identified parameter
name.
a mock code finding length of object1
inside controller like:
.controller('myctrl', function($scope){ $scope.object1; $scope.object2; $scope.mymethod = function(arg){ var length = $scope.arg.length; }; })
here, value passed argument 1 of object names this:
<button ng-click="mymethod(object1)">find length</button>
any appreciated, thanks.
you need use bracket notation when have dynamic key.
$scope.object1; $scope.mymethod = function (arg) { var length = $scope[arg].length; };
but have pass string function.
<button ng-click="mymethod('object1')">find length</button>
Comments
Post a Comment