Created
July 3, 2018 03:46
-
-
Save xjinza/8495f427b9d6b7f0aee1fb2f78aef1fc to your computer and use it in GitHub Desktop.
Injecting $state (ui-router) into $http interceptor causes circular dependency 拦截器里面引入$state #angularjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Injecting $state (ui-router) into $http interceptor causes circular dependency | |
angularjs拦截器里面引入$state 报错 | |
循环引用 | |
可以使用$injector | |
参考:https://stackoverflow.com/questions/20230691/injecting-state-ui-router-into-http-interceptor-causes-circular-dependency | |
var interceptor = ['$location', '$q', '$injector', function($location, $q, $injector) { | |
function success(response) { | |
return response; | |
} | |
function error(response) { | |
if(response.status === 401) { | |
$injector.get('$state').transitionTo('public.login'); | |
return $q.reject(response); | |
} | |
else { | |
return $q.reject(response); | |
} | |
} | |
return function(promise) { | |
return promise.then(success, error); | |
} | |
}]; | |
$httpProvider.responseInterceptors.push(interceptor); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment