33

How can I emit events from a factory or service. I am unable to inject $scope into the factory, thus unable to emit events.

I get the following error - Unknown provider: $scopeProvider <- $scope

Thanks, Murtaza

2
  • Why not watch a service variable in the controller(s) instead of emitting events? Dec 16, 2013 at 12:09
  • 1
    @AndersBornholm the watch functy in angular is triggered with every digest cycle. Digest cycles can happen quite often depending on the app. Pubsub offers better communication channel. It does not need to eval with every digest cycle. Mar 7, 2016 at 9:59

3 Answers 3

59

Inject $rootScope instead of $scope and then emit it on the $rootScope.

myApp.factory('myFactory', ['$rootScope', function ($rootScope) {
    $rootScope.$emit("myEvent", myEventParams);
}]);

Factories don't have access to the current controller/directive scope because there isn't one. They do have access to the root of the application though and that's why $rootScope is available.

1
  • 2
    This is the correct answer, Factory must not depend to a controller, since their live cycle is totally different. Oct 15, 2014 at 9:16
54

You cannot inject a controller's scope into a service. What you can do is:

  • pass the scope instance as a parameter to one of your service functions:

e.g.

app.factory('MyService', function() {

   return {
      myFunction: function(scope) {
         scope.$emit(...);
         ...
      }
    };
});
  • inject the $rootScope into your service:

e.g.

app.factory('MyService', ['$rootScope', function($rootScope) {

   return {
      myFunction: function() {
         $rootScope.$emit(...);
         ...
      }
    };
}]);
5
  • 15
    Would'nt I do a broadcast from rootscope?
    – murtaza52
    Dec 27, 2012 at 17:19
  • I think this is the only way to emit event upwards so that same level controllers don't catch emitted event - if that's what you want (it was in my case) Jul 4, 2013 at 19:09
  • @murtaza52 You really want to avoid broadcasting. See What's the correct way to communicate between controllers in AngularJS?
    – itsafire
    Feb 21, 2014 at 11:32
  • 1
    Not sure is the right way, this may create a leak when controller is destroyed, what happen to this $scope object? Oct 15, 2014 at 9:15
  • @Thomas Decaux I wouldn't use events in the first place (injecting services is the better way), but if you don't keep references to scope, it shouldn't create memory leaks
    – asgoth
    Oct 17, 2014 at 17:39
-1

In your factory inject $rootScope as-

myApp.factory('myFactory',function($rootScope){
return({
// use $rootScope as below to pass myEventParams to all below in hierarchy
$rootScope.$broadcast("myEvent",myEventParams);

})
}]);
2
  • 2
    I really do not understand voting an answer as not useful without giving proper justification . Highly Unprofessional . Jun 14, 2017 at 16:23
  • 5
    Well your answer is basically a duplicate of the 5 year old answers above it and doesn't add anything whilst also being less clear than the two existing accepted answers. What information did you think you were adding?
    – toxaq
    Jun 27, 2017 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.