从另一个控制器调用控制器中的一个函数

时间:2022-12-01 23:25:18

I need to have one controller in an angularjs file call a refresh on another controller in the same javascript file.

我需要在angularjs文件中有一个控制器在同一个javascript文件中的另一个控制器上调用刷新。

I have created a very simplified version of what I need in the following set of code. Basically when I click button 1 in the example below I want it to call the click1 function and then have the click1 function call click2 function on the 2nd controller. And then the opposite to be true when you click on the click2 function in the 2nd controller.

我在下面的代码集中创建了一个非常简化的版本。基本上当我单击下面示例中的按钮1时,我希望它调用click1函数,然后在第二个控制器上使用click1函数调用click2函数。当你点击第二个控制器中的click2功能时,情况恰恰相反。

HTML

<div ng-app>
    <div ng-controller="Control1">
        <div>{{count1}}</div>
        <button ng-click="click1()">Call both click methods from controller 1</button>
    </div>
    <div ng-controller="Control2">
        <div>{{count2}}</div>
        <button ng-click="click2()">Call both click methods from controller 2</button>
    </div>
</div>

Javascript

function Control1($scope) {
    $scope.count1 = 0;

    $scope.click1 = function () {
        $scope.count1++;
    }
}

function Control2($scope) {
    $scope.count2 = 0;

    $scope.click2 = function () {
        $scope.count2 = $scope.count2 + 2;
    }
}

I also created a jsfiddle for this.

我也为此创建了一个jsfiddle。

1 个解决方案

#1


2  

You are going to have to use scope broadcast or emit... Something like

你将不得不使用范围广播或发射...类似的东西

function Control1($scope, $rootScope) {
    $scope.count1 = 0;
    $scope.$on('clicked2', function() {
        $scope.count1++;
    });
    $scope.click1 = function () {
        $scope.count1++;
        $rootScope.$broadcast('clicked1');
    }
}

function Control2($scope, $rootScope) {
    $scope.count2 = 0;
    $scope.$on('clicked1', function() {
        $scope.count2 = $scope.count2 + 2;
    })
    $scope.click2 = function () {
        $scope.count2 = $scope.count2 + 2;
        $rootScope.$broadcast('clicked2');
    }
}

http://jsfiddle.net/jX3kh/ Read more on that here http://docs.angularjs.org/api/ng.$rootScope.Scope#methods_$broadcast .

http://jsfiddle.net/jX3kh/在这里阅读更多内容http://docs.angularjs.org/api/ng.$ro​​otScope.Scope#methods_$broadcast。

#1


2  

You are going to have to use scope broadcast or emit... Something like

你将不得不使用范围广播或发射...类似的东西

function Control1($scope, $rootScope) {
    $scope.count1 = 0;
    $scope.$on('clicked2', function() {
        $scope.count1++;
    });
    $scope.click1 = function () {
        $scope.count1++;
        $rootScope.$broadcast('clicked1');
    }
}

function Control2($scope, $rootScope) {
    $scope.count2 = 0;
    $scope.$on('clicked1', function() {
        $scope.count2 = $scope.count2 + 2;
    })
    $scope.click2 = function () {
        $scope.count2 = $scope.count2 + 2;
        $rootScope.$broadcast('clicked2');
    }
}

http://jsfiddle.net/jX3kh/ Read more on that here http://docs.angularjs.org/api/ng.$rootScope.Scope#methods_$broadcast .

http://jsfiddle.net/jX3kh/在这里阅读更多内容http://docs.angularjs.org/api/ng.$ro​​otScope.Scope#methods_$broadcast。