Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call AngularJs controller method from another Service

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

like image 843
Team Avatar asked May 13 '15 06:05

Team


1 Answers

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: {} });
like image 59
svarog Avatar answered Oct 18 '22 23:10

svarog