您现在的位置:首页 >> 前端 >> 内容

angular路由拦截

时间:2017/5/5 17:02:00 点击:

  核心提示:angular路由拦截 $scope.$on($stateChangeStart,function(event, toState, toParams, fromState, fromParams){ ...

angular路由拦截

 $scope.$on('$stateChangeStart',function(event, toState, toParams, fromState, fromParams){
       if(toState.name=='login') return;
        if(!comApi.isNotNullAndUndefined($sessionStorage.ustu)){
     event.preventDefault();// 取消默认跳转行为
      $state.go("login",{from:toState.name,w:'notLogin'});//跳转到登录界面
                }

            });

//登录控制器
 var from = $stateParams.from;
        console.log($stateParams);
         $state.go(from && from != "login" ? from : "app.home");

/**
 * Created by wanggs  */
//拦截http请求的拦截器
app.factory('factoryFilters', function ($sessionStorage, $location) {
    var sessionInjector = {
        request: function (config) {
            //请求之前
            //判断是否登陆.登陆后将token放到请求头部
            if ($sessionStorage.ustu) {
                config.headers["hahh"] = $sessionStorage.ustu;
            }
            return config;
        },
        response: function (config, header, header1, header2) {            //请求成功之后
            //从返回头部获跳转
            if (config.headers("hahh")) {
                $location.path(config.headers("hahh"));
            }
            return config;
        },
        requestError: function (config) {
        //请求之前错误
            return config;
        },
        responseError: function (config) {
            //请求之后错误
            return config;
        }
    };
    return sessionInjector;
});
app.config(['$httpProvider', function ($httpProvider) {
    $httpProvider.interceptors.push('factoryFilters');
}]);

Tags:AN NG GU UL 
作者:网络 来源:不详