美文网首页
angular监测$stateChangeStart变化会调用两

angular监测$stateChangeStart变化会调用两

作者: wuliJJ | 来源:发表于2016-08-08 17:39 被阅读157次

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变化会调用两

      本文链接:https://www.haomeiwen.com/subject/utyrsttx.html