在app.js我正在使用$ rootScope中的回调函数广播来获取第二个控制器上的方法。AngularJS:如何避免 - 从app.js调用的广播事件被触发两次?
function onSuccess(state) {
if (state != true) {
} else {
$rootScope.$broadcast('proximityCatched', null);
}
//console.log('Proximity state: ' + (state ? 'near' : 'far'));
};
在控制器我有监听器:
$rootScope.$on('proximityCatched', function() {
alert("TEST");
});
问题是,警报( “TEST”);被触发两次。
我试图找到任何工作解决方案使用stopPropagation或使用正常范围的广播,但没有运气。
我该如何以正确的方式做到这一点?
感谢您的任何建议。
编辑:路由器配置:
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('game', {
url: "/game",
templateUrl: "templates/game.html",
controller: "GameCtrl"
})
.state('home', {
url: "/home",
templateUrl: "templates/home.html",
controller: "HomeCtrl"
})
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/home');
});
你是否在路线和html中连接两次控制器? –
您是否认为我刚加入问题的路由器?控制器在这里提到,在index.html和ng-init – redrom
好的,你在html中使用'ng-controller'吗? –