2

I am trying to use eval from javascript to call a function in angular.js. I have its name in string

I used the following:

$scope.$eval("$scope.drawFactory.draw")({},$scope.paint); 

But it's showing

TypeError: $scope.$eval(...) is not a function

What is the proper way to do this?

Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
notnotundefined
  • 3,493
  • 3
  • 30
  • 39
  • 1
    duplication with: [enter link description here](http://stackoverflow.com/questions/15671471/angular-js-how-does-eval-work-and-why-is-it-different-from-vanilla-eval) – Oron Bendavid Mar 24 '16 at 12:45
  • If the format is the same you dont really need eval. You could do something like this: http://codepen.io/anon/pen/VabERy – Marie Mar 24 '16 at 12:47

1 Answers1

1

You could pass expression(string)/function to $eval which will evaluated against controller scope. No need to use $scope for $eval string.

$scope.$eval("drawFactory.draw")

Where as you are doing $eval function & again calling that function call by

//below is obviously gonna throw an error
$scope.$eval("$scope.drawFactory.draw")({},$scope.paint); //incorrect function execution
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299