12

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... :)

3 Answers 3

13

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

Sign up to request clarification or add additional context in comments.

Comments

4

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);}

2 Comments

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.
Thats fine. only you need is move that common function to a service and call it from required controllers.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.