Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

This question already has an answer here:

I need to call function in another controller in angular js.How it is possible way please help me thanks in advance

Code :

app.controller('One', ['$scope',
    function($scope) {
        $scope.parentmethod = function() {
            // task
        }
    }
]);
app.controller('two', ['$scope',
    function($scope) {
        $scope.childmethod = function() {
            // Here i want to call parentmethod of One controller
        }
    }
]);
share|improve this question

marked as duplicate by squiroid, Boaz, Pankaj Parkar, Duncan, Code Lღver Apr 6 '15 at 9:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
Put the function in a service, and inject the service in both controllers. Or, if the second controller scope is a child of the first controller scope, call it directly. – JB Nizet Apr 6 '15 at 7:47
1  
This feels like an XY Question to me; While there are a few ways you can call a function on another controller, there are really very few good reasons that you have to do so. Perhaps describing what your actual problem is would be more helpful than asking about the solution you are trying to implement? Calling a function on a different controller isn't really the "angular way" of doing things. – Claies Apr 6 '15 at 7:49
    
is one controller a child/grand-child of the other controller? Or are they unrelated siblings? – pixelbits Apr 6 '15 at 8:11
up vote 61 down vote accepted

Communication between controllers is done though $emit + $on / $broadcast + $on methods.

So in your case you want to call a method of Controller "One" inside Controller "Two", the correct way to do this is:

app.controller('One', ['$scope', '$rootScope'
    function($scope) {
        $rootScope.$on("CallParentMethod", function(){
           $scope.parentmethod();
        });

        $scope.parentmethod = function() {
            // task
        }
    }
]);
app.controller('two', ['$scope', '$rootScope'
    function($scope) {
        $scope.childmethod = function() {
            $rootScope.$emit("CallParentMethod", {});
        }
    }
]);

While $rootScope.$emit is called, you can send any data as second parameter.

share|improve this answer
    
Great!! it worked for me – Naveen May 16 at 12:54
    
I am able to call the function just using $rootScope.function_name(); – Nahiduzzaman Rose Dec 22 at 6:40

I wouldn't use function from one controller into another. A better approach would be to move the common function to a service and then inject the service in both controllers.

share|improve this answer

If the two controller is nested in One controller.
Then you can simply call:

$scope.parentmethod();  

Angular will search for parentmethod function starting with current scope and up until it will reach the rootScope.

share|improve this answer

The best approach for you to communicate between the two controllers is to use events.

See the scope documentation

In this check out $on, $broadcast and $emit.

share|improve this answer

You may use events to provide your data. Code like that:

app.controller('One', ['$scope', function ($scope) {
         $scope.parentmethod=function(){
                 $scope.$emit('one', res);// res - your data
         }
    }]);
    app.controller('two', ['$scope', function ($scope) {
         $scope.$on('updateMiniBasket', function (event, data) {
                ...
         });             
    }]);
share|improve this answer

If you would like to execute the parent controller's parentmethod function inside a child controller, call it:

$scope.$parent.parentmethod();

You can try it over here

share|improve this answer

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