I have one common controller this controller contain one method . I need to call this controller method from another service. Is it possible.
My Example code:
This is my Root Controller
and its contain 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 the function in root controller
app.factory('anotherService', function($rootScope,$broadcast){
$rootScope.$broadcast("func_call", { myParam: {} });
});
I tried to call the Root Controller function from service $rootScope.myFunction ("called")
but its not working . Any one please suggest better way to call the root controller function from service
you can always use $broadcast
, $emit
and $on
try going over the $rootScope docs
set up the receiving function listener in controller:
$scope.$on("func_call", function(event, args) {
var p = args.myParam;
// do something
})
and call it in your service:
$rootScope.$broadcast("func_call", { myParam: {} });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With