angular环境
电脑
用$rootScope.$on来监听事件的发生
app.run(['$rootScope', '$location' ,'$cookieStore', '$state', 'CacheManager', function($rootScope, $location, $cookieStore, $state,CacheManager){//监听路由事件$rootScope.$on('监控事件', function(event, toState, toParams, fromState, fromParams){ ... }}]);
监控的事件可以为:$stateChangeStart- 当模板开始解析之前触发$stateChangeSuccess- 当模板解析完成后触发$stateChangeError- 当模板解析过程中发生错误时触发$viewContentLoading- 当视图开始加载,DOM渲染完成之前触发,该事件将在$scope链上广播此事件。$viewContentLoaded- 当视图加载完成,DOM渲染完成之后触发,视图所在的$scope发出该事件。
使用event.preventDefault()可以阻止模板解析的发生
//event.preventDefault(); cancel url change $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){ event.preventDefault(); })
$rootScope.$on('$stateChangeStart',function(event, toState, toParams, fromState, fromParams){ console.log('$stateChangeStart'); }); $rootScope.$on('$stateChangeSuccess',function(event, toState, toParams, fromState, fromParams){ console.log('$stateChangeSuccess'); }); $rootScope.$on('$viewContentLoading',function(event, toState, toParams, fromState, fromParams){ console.log('$viewContentLoading'); }); $rootScope.$on('$viewContentLoaded',function(event, toState, toParams, fromState, fromParams){ console.log('$viewContentLoaded'); });结果如图。