多语言展示
当前在线:1466今日阅读:26今日分享:39

angular 路由监控

angular 路由监控
工具/原料
1

angular环境

2

电脑

方法/步骤
1

用$rootScope.$on来监听事件的发生

2

app.run(['$rootScope', '$location' ,'$cookieStore', '$state', 'CacheManager',  function($rootScope, $location, $cookieStore, $state,CacheManager){//监听路由事件$rootScope.$on('监控事件', function(event, toState, toParams, fromState, fromParams){ ... }}]);

3

监控的事件可以为:$stateChangeStart- 当模板开始解析之前触发$stateChangeSuccess- 当模板解析完成后触发$stateChangeError- 当模板解析过程中发生错误时触发$viewContentLoading- 当视图开始加载,DOM渲染完成之前触发,该事件将在$scope链上广播此事件。$viewContentLoaded- 当视图加载完成,DOM渲染完成之后触发,视图所在的$scope发出该事件。

4

使用event.preventDefault()可以阻止模板解析的发生

5

//event.preventDefault(); cancel url change $rootScope.$on('$stateChangeStart',    function(event, toState, toParams, fromState, fromParams){           event.preventDefault();       })

6

$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');  });结果如图。

推荐信息