angular/angular.js — Issue #595: Get service instance from the "global" scope
Author: lukaszdechovan
Created: 2011-10-14 08:39:28+00:00
State: open

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.