angular利用ui-router进行登录检查

SAP都会有这个问题,session过期或者页面被刷新的情况下应该进入登录页.

监听ui-router的satte事件可以实现当state切换的时候检查登录情况

监听state变化处理页面刷新

    //添加事件监听
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
if (toState.name == 'register') return; // 如果是进入登录界面则允许
// 如果用户不存在
if (!$rootScope.session || !$rootScope.seccion.session_id) {
event.preventDefault(); // 取消默认跳转行为
$state.go("register", { from: fromState.name, w: 'notLogin' }); //跳转到登录界面
}
});

HttpProvider的Interceptor处理过期session

intercepto拦截器,可以处理所有的请求结果,能够实现:基于状态码统一的错误处理

如何注册interceptor:https://docs.angularjs.org/api/ng/service/$http#interceptors

//register interceptor as sercice
app.factory('UserInterceptor', ["$q","$rootScope",function ($q,$rootScope) {
return {
request:function(config){
config.headers["TOKEN"] = $rootScope.user.token;
return config;
},
responseError: function (response) {
var data = response.data;
// 判断错误码,如果是未登录
if(data["errorCode"] == "500999"){
// 清空用户本地token存储的信息
$rootScope.user = {token:""};
// 全局事件,方便其他view获取该事件,并给以相应的提示或处理
$rootScope.$emit("userIntercepted","notLogin",response);
}
// 如果是登录超时
if(data["errorCode"] == "500998"){
$rootScope.$emit("userIntercepted","sessionOut",response);
}
return $q.reject(response);
}
};
}]); //push interceptor to interceptor list
app.config(function ($httpProvider) {
$httpProvider.interceptors.push('UserInterceptor');
}); $rootScope.$on('userIntercepted',function(errorType){
// 跳转到登录界面,这里我记录了一个from,这样可以在登录后自动跳转到未登录之前的那个界面
$state.go("login",{from:$state.current.name,w:errorType});
});

相关

UI-State Events

$stateChangeCancel

$stateChangeError

$stateChangeStart

$stateChangeSuccess

$stateNotFound

Directive Hooks

详情:https://docs.angularjs.org/api/ng/service/$compile#life-cycle-hooks

angular.module('do-check-module', [])
.component('app', {
template:
'Month: <input ng-model="$ctrl.month" ng-change="$ctrl.updateDate()">' +
'Date: {{ $ctrl.date }}' +
'<test date="$ctrl.date"></test>',
controller: function() {
this.date = new Date();
this.month = this.date.getMonth();
this.updateDate = function() {
this.date.setMonth(this.month);
};
}
})
.component('test', {
bindings: { date: '<' },
template:
'<pre>{{ $ctrl.log | json }}</pre>',
controller: function() {
var previousValue;
this.log = [];
//hook here
this.$doCheck = function() {
var currentValue = this.date && this.date.valueOf();
if (previousValue !== currentValue) {
this.log.push('doCheck: date mutated: ' + this.date);
previousValue = currentValue;
}
};
}
});

参考:

https://ui-router.github.io/docs/latest/modules/ng1_state_events.html

https://github.com/angular-ui/ui-router/wiki#state-change-events

http://www.brafox.com/post/2015/javascript/angularjs/angularjs-router-interceptor.html

angular利用ui-router登录检查的更多相关文章

  1. angular 的ui.router 定义不同的state 对应相同的url

    Angular UI Router: Different states with same URL? The landing page of my app has two states: home-p ...

  2. angular中的 登录检查 和 过期Session清理

    angular利用ui-router进行登录检查 SAP都会有这个问题,session过期或者页面被刷新的情况下应该进入登录页. 监听ui-router的satte事件可以实现当state切换的时候检 ...

  3. AngularJS 使用 UI Router 实现表单向导

    Today we will be using AngularJS and the great UI Router and the Angular ngAnimate module to create ...

  4. angular : $location & $state(UI router)的关系

    次序:angular 的 location会先跑 $rootScope.$on("$locationChangeStart", function (scope, newUrl, o ...

  5. angular ui.router 路由传参数

    angular已经用了一段时间了,最近在做路由,做一下笔记. 路由跳转的时候进行穿参 ui.router方式 <a ui-sref="edit({id:5})"> 编辑 ...

  6. angular路由——ui.route

    angular路由 使用案例 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  7. 【原创】ui.router源码解析

    Angular系列文章之angular路由 路由(route),几乎所有的MVC(VM)框架都应该具有的特性,因为它是前端构建单页面应用(SPA)必不可少的组成部分. 那么,对于angular而言,它 ...

  8. ngRoute 与ui.router区别

    angular路由 路由 (route) ,几乎所有的 MVC(VM) 框架都应该具有的特性,因为它是前端构建单页面应用 (SPA) 必不可少的组成部分. 那么,对于 angular 而言,它自然也有 ...

  9. Angular.js之Router学习笔记

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

随机推荐

  1. GIT FLOW 时序图

    git flow sequence md link: git branching model master->master branch: use default branch Note rig ...

  2. Vertica数据查询优化

    vertica是惠普公司推出的列式分布式数据库,在OLAP领域有其独到的地方,目前社区版免费,但是只能存放1T的数据.我在工作中维护的bi系统后端就是使用的vertica数据库,平时也经常需要对于数据 ...

  3. 使用sklearn优雅地进行数据挖掘

    目录 1 使用sklearn进行数据挖掘 1.1 数据挖掘的步骤 1.2 数据初貌 1.3 关键技术2 并行处理 2.1 整体并行处理 2.2 部分并行处理3 流水线处理4 自动化调参5 持久化6 回 ...

  4. angular中的 ng-change

    <!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...

  5. Mac:文件夹树型展示 tree

    目标: 想要在MAC的Terminal中查看文件夹中所有文件的树型结构及文件夹.文件树统计. 安装方法: 1.brew安装 官网:http://brew.sh/ brew是Mac中安装软件的神器,一定 ...

  6. [UCSD白板题] Compute the Edit Distance Between Two Strings

    Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...

  7. 结对开发训练(续)(郭林林&胡潇丹)

    本次题目:求二维数组最大连续的子数组之和. 通过前两次对问题的分析,这次在拿到题目时,我们首先与前两次题目做对比,尤其与第二次的题目相比较,这是在第二次题目上的扩展,第二次的题目是此次题目的一个特例. ...

  8. 第七章 内存管理单元MMU介绍

    7.1 内存管理单元MMU介绍 7.1.1 S3C2410/S3C2440 MMU特性 负责虚拟地址到物理地址的映射,并提供硬件机制的内存访问权限检查 特性: 与ARM V4兼容的映射长度.域.访问权 ...

  9. [原] XAF How to bind a stored procedure to a ListView in XAF

    First, I suggest that you review the following topic to learn how to show a custom set of objects in ...

  10. seajs模块化作用理解(一句话)

    seajs是js模块化的工具,主要大文件js不方便其他人理解,加载也较慢,seajs把各个功能模块分开,方便平行化开发,同时易于修改和理解,不用重复写功能需要时就应用 (有什么错误,请指正,缺少多谢补 ...