2

我有以下内容:

<div data-ng-controller="Controller1">
   <div class="button" 
        data-ng-click="action2()">
   </div>
</div>

<div data-ng-controller="Controller2">

</div>
angular.module('test')
   .controller('QuestionsStatusController1',
    ['$rootScope', '$scope',
    function ($rootScope, $scope) {

    }]);

angular.module('test')
   .controller('QuestionsStatusController2',
    ['$rootScope', '$scope',
    function ($rootScope, $scope) {

         // I need some way to receive the action2()

    }]);

有没有办法我可以在 HTML 中单击Controller1导致方法触发Controller2。请注意,我没有这些控制器中的任何一个的父控制器。那是因为我使用的是ui-router.

4

1 回答 1

3
angular.module('test')
    .controller('QuestionsStatusController1',
    ['$rootScope', '$scope', '$resource', '$state',
    function ($rootScope, $scope, $resource, $state) {

        $scope.action2 = function() {
            $rootScope.$broadcast('action2@QuestionStatusController1');
        }

    }]);

angular.module('test')
   .controller('QuestionsStatusController2',
   ['$rootScope', '$scope', '$resource', '$state',
   function ($rootScope, $scope, $resource, $state) {

    $rootScope.$on('action2@QuestionStatusController1', function {
         //write your listener here
    })

   }]);
于 2013-09-17T17:27:04.793 回答