Angular.js: Get service instance from the "global" scope

Created on 14 Oct 2011  路  7Comments  路  Source: angular/angular.js

Hello,

If I inject angular service into a controller, the angular creates new instance and put it into instanceCache object... so there is only one instance of the service even if I use more controllers (it's a singleton).

However, I would like to get that one instance from instanceCache outside the controller.
I know there is a possibility how to achieve this within new angular.scope()....

var scope = angular.scope();
var myService = scope.$service('Id of myService under which it was registered'); 

.... but this code creates new instance of myService so if I inject myService also into controller, now I have 2 separate instances in the application.

Is there any way how can I achieve this with angular.js?
I tried also angular.element('selector to whatever').scope() is always undefined.... I hoped I can access the scope of the controller and get service from that scope.

Most helpful comment

For anyone coming from google: this solution appears to be outdated. rootScope.$service is undefined now.

The current answer to this is:

injector = angular.element(document).injector()
service = injector.get('theService')

All 7 comments

this can be done as follows:

var scope = angular.element(document); //or some other element of the compiled template
var myService = scope.$service('myService');

Again, please ask on mailing list first... http://groups.google.com/group/angular

So you should be able to do it like this

angular.element('selector').scope().$service('myServiceId');

However, the scope is not created until after the document load event.

It may be easier to just publish the service to the window.

Better question is: What are you trying to achieve, this seems to go against
the zen of angular.

-- misko

2011/10/14 lukaszdechovan <
[email protected]>

Hello,

If I inject angular service into a controller, the angular creates new
instance and put it into instanceCache object... so there is only one
instance of the service even if I use more controllers (it's a singleton).

However, I would like to get that one instance from instanceCache outside
the controller.
I know there is a possibility how to achieve this within new
angular.scope()....

var scope = angular.scope();
var myService = scope.$service('Id of myService under which it was
registered');

.... but this code creates new instance of myService so if I inject
myService also into controller, now I have 2 separate instances in the
application.

Is there any way how can I achieve this with angular.js?
I tried also angular.element('selector to whatever').scope() is always
undefined.... I hoped I can access the scope of the controller and get
service from that scope.

Reply to this email directly or view it on GitHub:
https://github.com/angular/angular.js/issues/595

For anyone coming from google: this solution appears to be outdated. rootScope.$service is undefined now.

The current answer to this is:

injector = angular.element(document).injector()
service = injector.get('theService')

Had the same problem,(I use Typescript so I had to get a service instance and assign it to a static class variable),
For me that solved it.You need to specify the module in which the service you are trying to get is registered.

SERVICE_REGISTERED_IN_myMOD =angular.injector(['myMOD', 'ng']).get('SERVICE_REGISTERED_IN_myMOD'); 

@drumaddict Thanks for that comment :+1:

I don't understand why, but @drumaddict's solution didn't work until I flipped the modules listed in the injector method call:

SERVICE_REGISTERED_IN_myMOD =angular.injector(['ng', 'myMOD']).get('SERVICE_REGISTERED_IN_myMOD');
Was this page helpful?
0 / 5 - 0 ratings