Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Angular service by name programmatically

I have a string that contains the name of a service I need to inject into in one of my controllers programmatically.

function Ctrl() {
    var serviceName = '$myService';
    var service = ???
}

I am assuming it should be pretty easy to do something similar to:

function Ctrl($injector) {
    var serviceName = '$myService';
    var service = $injector.inject(serviceName);
}

I know I don't have the syntax correct but can someone give me the correct syntax?

like image 804
Leslie Hanks Avatar asked Oct 08 '13 22:10

Leslie Hanks


1 Answers

You can get a service like this:

var myService = $injector.get('MyServiceName');

like image 177
Foo L Avatar answered Oct 05 '22 01:10

Foo L