Angular 其他控制器未收到广播事件
Angular broadcast event not received in other controller
头控制器
mapsModule = angular.module('maps-module');
mapsModule.controller('psHeaderCtrl', function($scope, $rootScope, MESSAGES,
psHttpSrvc){
$scope.addAlertPin = function(){
console.log('broadcast event');
$scope.$broadcast('event1', { data: 'asdasdasasda'});
}
});
地图控制器
map = angular.module('maps-module');
map.controller('map_controller',function
($scope,$rootScope,$timeout) {
$scope.$on('event1', function(event, args){
console.log('asdasdasd');
});
});
Header.html
<div ng-controller="psHeaderCtrl">
<ul class="nav navbar-toolbar navbar-right navbar-toolbar-right">
<li class="nav-item" id="plus">
<a class="nav-link" data-toggle="" href="javascript:void(0)"
title="plus" ng-click="addAlertPin()">
<i class="icon wb-plus" aria-hidden="true"></i>
</a>
</li>
</ul>
</div>
包含所有文件。 Angular 工作正常,但未收到广播事件。
尝试使用$rootScope
广播事件:
$scope.addAlertPin = function(){
console.log('broadcast event');
$rootScope.$broadcast('event1', { data: 'asdasdasasda'});
}
头控制器
mapsModule = angular.module('maps-module');
mapsModule.controller('psHeaderCtrl', function($scope, $rootScope, MESSAGES,
psHttpSrvc){
$scope.addAlertPin = function(){
console.log('broadcast event');
$scope.$broadcast('event1', { data: 'asdasdasasda'});
}
});
地图控制器
map = angular.module('maps-module');
map.controller('map_controller',function
($scope,$rootScope,$timeout) {
$scope.$on('event1', function(event, args){
console.log('asdasdasd');
});
});
Header.html
<div ng-controller="psHeaderCtrl">
<ul class="nav navbar-toolbar navbar-right navbar-toolbar-right">
<li class="nav-item" id="plus">
<a class="nav-link" data-toggle="" href="javascript:void(0)"
title="plus" ng-click="addAlertPin()">
<i class="icon wb-plus" aria-hidden="true"></i>
</a>
</li>
</ul>
</div>
包含所有文件。 Angular 工作正常,但未收到广播事件。
尝试使用$rootScope
广播事件:
$scope.addAlertPin = function(){
console.log('broadcast event');
$rootScope.$broadcast('event1', { data: 'asdasdasasda'});
}