angular监测$stateChangeStart变化会调用两次的问题

Your controller is instantiated by ui-router every time you enter the state it is associated with. Therefore, your $rootScope.$on call will be adding a new listener to the $stateChangeStart event each time you enter that state.

If you only need to handle the event once per controller instance, you can save the deregister function that $rootScope.$on returns and execute it from within the listener callback.

var deregisterStateChangeStart = $rootScope.$on('$stateChangeStart', function (event) {
// Do something here.
deregisterStateChangeStart();
});

监测路由地址变化,将函数保存下来,但后放在回调里面,这样就只调用一次。否则就会调用两次。

你可能感兴趣的:(angular监测$stateChangeStart变化会调用两次的问题)