I am new to using angular js and i have declare many controller and now i want to user function of one controller into another controller. here is my sample code.

app.controller('Controller1',function($scope,$http,$compile){
    $scope.test1=function($scope)
    {
          alert("test1");
    }
});

app.controller('Controller2',function($scope,$http,$compile){
    $scope.test2=function($scope)
    {
          alert("test1");
    }
});
app.controller('Controller3',function($scope,$http,$compile){
  ///
});

Now i want to call test2 function inside controller3. Can anybody help.. Thanks in Avance... :)

share|improve this question

You can't call a method from a controller within a controller. You will need to extract the method out, create a service and call it. This will also decouple the code from each other and make it more testable

(function() {
    angular.module('app', [])
        .service('svc', function() {
            var svc = {};

            svc.method = function() {
                alert(1);
            }

            return svc;
        })
        .controller('ctrl', [
            '$scope', 'svc', function($scope, svc) {
                svc.method();
            }
        ]);
})();

Example: http://plnkr.co/edit/FQnthYpxgxAiIJYa69hu?p=preview

share|improve this answer
    
Thanks For Help :) – Mayur Aug 29 '14 at 7:06

Best way is write a service and use that service in both controllers. see the documentation Service documentation

If you really want to access controller method from another controller then follow the below option: emitting an event on scope:

function FirstController($scope) {  $scope.$on('someEvent', function(event, args) {});}

function SecondController($scope) {  $scope.$emit('someEvent', args);}
share|improve this answer
    
Yes But i we have already written code with lots of controller so now i have to call one function which is in another controller. – Mayur Aug 29 '14 at 6:48
1  
Thats fine. only you need is move that common function to a service and call it from required controllers. – Seminda Aug 29 '14 at 7:01

The controller is a constructor, it will create a new instance if for example used in a directive.

You can still do what you wanted, assuming that your controllers are in the same scope, just do:

Note they must be in the same scope, could still work if a child scope was not isolated. The directive's definition:

{
    controller: Controller1,
    controllerAs: 'ctrl1',
    link: function($scope) {

        $scope.ctrl1.test1(); // call a method from controller 1
        $scope.ctrl2.test2(); // created by the directive after this definition
        $scope.ctrl3.test3(); // created by the directive after this definition
    }
}

....
{
    controller: Controller2,
    controllerAs: 'ctrl2',
    link: function($scope) {
        $scope.ctrl1.test1(); // created earlier
        $scope.ctrl2.test2(); // ...
        $scope.ctrl3.test3(); // created by the directive after this definition
    }
}

....
{
    controller: Controller3,
    controllerAs: 'ctrl3',
    link: function($scope) {
        $scope.ctrl1.test1();
        $scope.ctrl2.test2();
        $scope.ctrl3.test3();
    }
}

This should work.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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