Angularjs $rootScope.$broadcast on$rootScope.$on:RangeError:超过最大调用堆栈大小

Angularjs $rootScope.$broadcast on$rootScope.$on:RangeError:超过最大调用堆栈大小,angularjs,events,event-handling,angularjs-scope,angularjs-rootscope,Angularjs,Events,Event Handling,Angularjs Scope,Angularjs Rootscope,我想以如下方式定义页面标题: 标准的PageController: angular.module('admin').controller('AdminController', function($scope) { // emit an event to rootscope $scope.$emit('setPageTitle','Administration'); } ); angular.module('core').controller('

我想以如下方式定义页面标题:

标准的
PageController

angular.module('admin').controller('AdminController',
    function($scope) {

        // emit an event to rootscope
        $scope.$emit('setPageTitle','Administration');
    }
);
angular.module('core').controller('PageHeaderController',
    function($scope) {

        $scope.$on('setPageTitle',function(evt, title){
            $scope.pageTitle = title;
        });
    }
);
然后在运行块中:

angular.module('core').run(function($rootScope){
    $rootScope.$on('setPageTitle',function(evt,title){
        $rootScope.$broadcast('setPageTitle',title);   // The error is thrown from this line
    });
});
最后在
PageHeaderController
中:

angular.module('admin').controller('AdminController',
    function($scope) {

        // emit an event to rootscope
        $scope.$emit('setPageTitle','Administration');
    }
);
angular.module('core').controller('PageHeaderController',
    function($scope) {

        $scope.$on('setPageTitle',function(evt, title){
            $scope.pageTitle = title;
        });
    }
);
这样,我就不需要在每个
PageController
中注入
$rootScope
,而只需要在其他任务中经常使用的
$scope

但是我在上面第二个代码块中标记的行中得到了这个错误

RangeError:超出了最大调用堆栈大小

这里怎么了?我看不出是什么导致了无穷循环,因为我想我只是做了以下步骤:

  • 从孩子身上散发
  • 在rootscope中处理并向儿童广播
  • 在特定子对象中处理
这是原因吗?尝试按跟踪方向指定不同的事件名称


这是原因吗?尝试按跟踪方向指定不同的事件名称。

将“
setPageTitle
”事件名称更改为其他名称应该可以工作,如下所示

angular.module('core').run(function($rootScope){
    $rootScope.$on('setPageTitle',function(evt,title){
        $rootScope.$broadcast('setPageTitleCtrl',title);   // The error is thrown from this line - changed 'setPageTitle' to 'setPageTitleCtrl'
    });
});
控制器:

angular.module('core').controller('PageHeaderController',
    function($scope) {

        $scope.$on('setPageTitleCtrl',function(evt, title){
            $scope.pageTitle = title;
        });
    }
)

将“
setPageTitle
”事件名称更改为其他名称应该可以工作,如下所示

angular.module('core').run(function($rootScope){
    $rootScope.$on('setPageTitle',function(evt,title){
        $rootScope.$broadcast('setPageTitleCtrl',title);   // The error is thrown from this line - changed 'setPageTitle' to 'setPageTitleCtrl'
    });
});
控制器:

angular.module('core').controller('PageHeaderController',
    function($scope) {

        $scope.$on('setPageTitleCtrl',function(evt, title){
            $scope.pageTitle = title;
        });
    }
)

这里的原因是$rootScope能够捕获自己广播的事件。于是无限循环发生了


这里的原因是$rootScope能够捕获自己广播的事件。于是无限循环发生了


将“setPageTitle”事件名称更改为其他名称将“setPageTitle”事件名称更改为其他名称