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();
});

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容