Convert string to function in AngularJS

I am trying to convert strings to calls to the AngularJS Service method in a controller. For example, I would like to convert the string "Contact.send (email)" to call an existing service method. I thought to use:

window["Contact"]["send"](email);

like in this thread - How to execute a JavaScript function when I have its name as a string , but it says that the contact service is undefined, despite being injected into the controller.

+4
source share
2 answers

You need to use $ injector to get the service from the line:

$injector.get('Contact')['send'](email);
+6
source

$scope. $eval $scope.

$scope.$eval("Contact.send(email)");

, Contact $scope, . . https://code.angularjs.org/1.2.15/docs/api/ng/type/ $rootScope.Scope

+6

All Articles