javascript - calling function when function name is json value -


this question has answer here:

i have json object

var json={   key1:val1,   key2:"foo" //as string } 

and have foo function

$scope.foo=function{   //something } 

now on ng-click want ng-click="json.key2()"

basically want call foo function it's string name ?

is possible ? know it's bad approach, possible ?

thanks

you can access properties on object dynamically using square-brackets.

for example, in controller (after declaration of $scope.foo):

$scope.fn = $scope[json.key2]; 

and in markup:

ng-click="fn()" 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -