在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'嗎? –