Skip to content

Instantly share code, notes, and snippets.

@xjinza
Created July 3, 2018 03:46
Show Gist options
  • Save xjinza/8495f427b9d6b7f0aee1fb2f78aef1fc to your computer and use it in GitHub Desktop.
Save xjinza/8495f427b9d6b7f0aee1fb2f78aef1fc to your computer and use it in GitHub Desktop.
Injecting $state (ui-router) into $http interceptor causes circular dependency 拦截器里面引入$state #angularjs
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