简体   繁体   English

从另一个Controller Angular Js调用函数

[英]Call function from another Controller Angular Js

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. 我是新手使用角度js,我已经声明了许多控制器,现在我想将一个控制器的功能用于另一个控制器。 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. 现在我想在controller3中调用test2函数。 Can anybody help.. Thanks in Avance... :) 任何人都可以帮助..感谢Avance ... :)

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 示例: http//plnkr.co/edit/FQnthYpxgxAiIJYa69hu?p = preview

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

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. 这应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM