javascript - How to call a function on the scope from a string value -
i have object containing array of strings
$scope.actions=[ "add_inscription", "add_tools", "add_instruction", "remove_inscription", "remove_tools", "remove_instruction" ];
and able dynamic action calls through delegating function..
$scope.delegate = function () { var arg = arguments[0]; ( key in $scope.actions ) { if ($scope.actions[key] == arg ) { // call function has matching name } } }
so in template have this
<button ng-click="delegate('add_inscription')">add inscription</button>
i don't know if thinking in right direction either,, point actions object pretty large , don't want write massive switch case statement have update time.
is there way in angular?
i have no problem doing in straight javascript
var fnstring = "add_inscription"; // find object var fn = window[fnstring]; // if object function if (typeof fn === "function") fn();
but in angular can't done..
assuming "actions" functions defined inside scope, like:
$scope.add_inscription = function(){ ... }
you should do:
var _action = 'add_inscription'; $scope[_action]();
Comments
Post a Comment