Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
87
rated 0 times [  90] [ 3]  / answers: 1 / hits: 9185  / 10 Years ago, fri, july 18, 2014, 12:00:00

I am trying to convert strings to AngularJS Service method calls 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);



as 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.


More From » angularjs

 Answers
18

You need to use $injector to get a service from a string:



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

[#43796] Wednesday, July 16, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dayana

Total Points: 302
Total Questions: 102
Total Answers: 100

Location: Cayman Islands
Member since Fri, Mar 4, 2022
2 Years ago
;