最佳答案
我想达到的目标
如果 $http 请求返回401错误,我想转换到某种状态(登录)。因此,我创建了一个 $http 拦截器。
问题是
When I am trying to insert '$state' into the interceptor I get a circular dependency. Why and how do i fix it?
密码
//Inside Config function
var interceptor = ['$location', '$q', '$state', function($location, $q, $state) {
function success(response) {
return response;
}
function error(response) {
if(response.status === 401) {
$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);