angular 监听路由状态变化

app.run(['$rootScope', '$location' ,'$cookieStore', '$state', 'CacheManager',  function($rootScope, $location, $cookieStore, $state,CacheManager){
//监听路由事件

        $rootScope.$on('$stateChangeStart',

            function(event, toState, toParams, fromState, fromParams){

                if(toState.name=="tabs.post"&&fromState.name=="tabs.orderList"){

                     //$location.path();//获取路由地址

                    $location.path('/tabs/home');//设置路由地址

                }

        })

}]);

ps:

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

 $rootScope.$on('$stateChangeStart',

            function(event, toState, toParams, fromState, fromParams){

                event.preventDefault();

        })


  • $stateNotFound-v0.3.0- 在 transition 时通过状态名查找状态,当状态无法找到时发生。该事件在 scope 链上广播,只允许一次处理错误的机会。unfoundState将作为参数传入事件监听函数,下面例子中可以看到unfoundState的三个属性。使用event.preventDefault()来阻止模板解析,

// somewhere, assume lazy.state has not been defined$state.go("lazy.state", {a:1, b:2}, {inherit:false});// somewhere else$scope.$on('$stateNotFound',function(event, unfoundState, fromState, fromParams){console.log(unfoundState.to); // "lazy.state"console.log(unfoundState.toParams); // {a:1, b:2}console.log(unfoundState.options); // {inherit:false} + default options})
  • $stateChangeSuccess- 当模板解析完成后触发

$rootScope.$on('$stateChangeSuccess',function(event, toState, toParams, fromState, fromParams){ ... })
  • $stateChangeError- 当模板解析过程中发生错误时触发

$rootScope.$on('$stateChangeError',function(event, toState, toParams, fromState, fromParams, error){ ... })

View Load Events 视图加载事件

  • $viewContentLoading- 当视图开始加载,DOM渲染完成之前触发,该事件将在$scope链上广播此事件。

$scope.$on('$viewContentLoading',function(event, viewConfig){// Access to all the view config properties.// and one special property 'targetView'// viewConfig.targetView });
  • $viewContentLoaded- 当视图加载完成,DOM渲染完成之后触发,视图所在的$scope发出该事件。

$scope.$on('$viewContentLoaded',function(event){ ... });
$scope.$watch('$viewContentLoaded',function(event){ ... });

猜你喜欢

转载自blog.csdn.net/tdjqqq/article/details/52228460