ducin
ducin

Reputation: 26467

angular.injector - can't find custom (app-related) services

I've got a angular application. I start it and I open the chrome dev tools console and start typing.

I want to do some $injector.get(...), to play with my services and factories in console. But I'm not in a DI context, so... $injector is the same as angular.injector(), right? So I type angular.injector().get('MyModel') and I get

Uncaught Error: [$injector:unpr] Unknown provider: MyModelProvider <- MyModel

Well... why does angular injector can't find the model that I have declared with:

MyModule.service('MyModel', MyModel);

and I'm sure it works, because when I click out some things in the app, the model gets used and calls the API correctly.

Moreover, if I execute angular.injector().get('$attrs') or angular.injector().get('$scope') or whatever, I get the same errors. So I can see that I can't use injector this way.

What am I doing wrong? Or is there something wrong with my app configuration?

More or less, what I want to do is to retrieve a service or a factory or whatever from angular in the console (when the app is already loaded) to play with those DI-components.

Upvotes: 1

Views: 203

Answers (1)

Razvan B.
Razvan B.

Reputation: 6771

Try :

angular.element(document.body).injector().get('MyModel')

For any DOM element you can do this:

  • angular.element(domElement).scope() to get the current scope for the element
  • angular.element(domElement).injector() to get the current app injector
  • angular.element(domElement).controller() to get a hold of the ng-controller instance.

Upvotes: 2

Related Questions