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-
$stateChangeSuccess- 当模板解析完成后触发
$stateChangeError- 当模板解析过程中发生错误时触发
View Load Events 视图加载事件
$viewContentLoading- 当视图开始加载,DOM渲染完成之前触发,该事件将在
$viewContentLoaded- 当视图加载完成,DOM渲染完成之后触发,视图所在的
ps:参考文献
http://www.aichengxu.com/view/44576
$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
相关文章推荐
- angularjs应用骨架(4)
- AngularJs中文社区学习资料
- Angularjs,WebAPI 搭建一个简易权限管理系统 —— Angularjs 前端主体结构(五)
- Angularjs,WebAPI 搭建一个简易权限管理系统 —— WebAPI项目主体结构(四)
- Angularjs,WebAPI 搭建一个简易权限管理系统 —— 系统业务与实现(三)
- AngularJS学习(三)
- angular启动过程分析
- AngularJS--begining
- 【AngularJS】【02】AngularJS应用骨架
- AngularJS中$scope用法
- angularjs应用骨架(3)
- 【AngularJS】【01】简介
- Angularjs-Dirty Checking
- Angular $parse
- 七步从AngularJS菜鸟到专家(7):Routing
- 七步从AngularJS菜鸟到专家(6):服务
- angular表单验证
- AngularJS的更高一步见解
- AngularJs: Reload page
- Angularjs,WebAPI 搭建一个简易权限管理系统