0

I have a json object like

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

and i have foo function

$scope.foo=function{
  //something
}

Now on ng-click I want to do something like ng-click="json.key2()"

Basically I want to call this foo function from it's string name ?

Is it possible ? I know it's bad approach, but is it possible ?

Thanks

Aishwat Singh
  • 4,331
  • 2
  • 26
  • 48

1 Answers1

1

You can access properties on an object dynamically by using square-brackets.

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

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

And in your markup:

ng-click="fn()"
sdgluck
  • 24,894
  • 8
  • 75
  • 90
  • ok , i took json also on scope like `$scope.json={....}`then i keep key2 as string only but later down , i make it like $scope.json.key2=$scope.foo – Aishwat Singh Feb 06 '16 at 17:16