Call AngularJs controller method from another service

I have one common controller, this controller contains one method. I need to call this controller method from another service. Is it possible.

My code example: This is my Root Controller and its containing function called myFunction

  app.controller('rootController', function($rootScope,$scope,$on) { $scope.$on("func_call", function(event, args) { var p = args.myParam; // do something }) }); 

This is my service, from this service I need to call a function in the root controller

  app.factory('anotherService', function($rootScope,$broadcast){ $rootScope.$broadcast("func_call", { myParam: {} }); }); 

I tried calling the Root Controller function from the $rootScope.myFunction ("called") , but it did not work. Anyone please suggest a better way to call the root controller function from the service

+5
source share
1 answer

you can always use $broadcast , $emit and $on

try going through $ rootScope docs

configure the receiver of the receive function in the controller:

 $scope.$on("func_call", function(event, args) { var p = args.myParam; // do something }) 

and name it in your service:

 $rootScope.$broadcast("func_call", { myParam: {} }); 
+1
source

All Articles