从另一个控制器调用函数

时间:2022-03-09 12:34:39

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.

我对使用角度js很陌生,我已经声明了很多控制器现在我想把一个控制器的功能用在另一个控制器上。这是我的示例代码。

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中调用test2函数。有人能帮助. .感谢皇冠……:)

3 个解决方案

#1


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

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

#2


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

#3


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.

这应该工作。

#1


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

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

#2


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

#3


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.

这应该工作。