您的位置:首页 > Web前端 > AngularJS

Angular 监听路由变化事件

2015-07-16 00:00 591 查看
摘要: $stateChangeStart- 当模板开始解析之前触发
$rootScope.$on('$stateChangeStart',
function(event, toState, toParams, fromState, fromParams){ ... })

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){ ... });

ps:参考文献
http://www.aichengxu.com/view/44576
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: