Ben Aston
Ben Aston

Reputation: 55759

Retrieving an instance from the Angular injector at runtime

When my application is running, I would like to open the console and get hold of an object instance from the injector. Is this possible?

Something like:

var service = angular.injector.get('my-service'); // This does not work

Upvotes: 1

Views: 174

Answers (1)

dfsq
dfsq

Reputation: 193291

Yes, it is possible:

var service = angular.element(document.documentElement).injector().get('my-service');

In above snippet you should pass application root DOM element into angular.element. For this example, I used document.documentElement which is html element.

Also note, that you can't use angular.injector() directly, as you need to use the injector instance used for application boostraping. To retrieve this injector instance object you need to call injector method of the corresponding angular.element instance.

Upvotes: 1

Related Questions