nicholas
nicholas

Reputation: 14563

Inject AngularJS service into global scope

Is it possible to get an angular js service into the global scope to use in the JS Debugging console? What I'm going for is some very low tech, quick and dirty, testing. Something like:

var myService = angular.injector().get('myService');
myService.doSomething();

and have it do something.

But I keep getting

Error: Unknown provider: myServiceProvider <- myService

Upvotes: 0

Views: 1091

Answers (1)

joakimbl
joakimbl

Reputation: 18081

In your console you can do something like this:

var domElement = document.getElementById('elementInApp');//
var el = angular.element(dom);
var myService = el.get('myService');
myService.doSomething();

This will allow you to use the existing application injector instead of creating a new (which is basically what you are doing).

//you can also do this
var newMyService = angular.injector(['moduleWithMyService']).get('myService');
//note that this creates a new 'myService' so
newMyService === myService; //false

Upvotes: 6

Related Questions