Search code examples
angularjsangularjs-service

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

Solution

  • 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