【发布时间】:2015-08-05 07:56:27
【问题描述】:
我尝试在 http 拦截器中执行 http.post 调用,但得到一个
Circular dependency found: $http <- Ace <- authInterceptor <- $http <- $templateRequest <- $compile
我明白了原因,但我不知道如何解决它... 仍然是 Angular 的新手,有时会有点困惑,我希望你能帮助我:) 这是我的代码:
var app = angular.module('AceAngularApi', []);
app.service('Ace', ['$http', '$q', '$injector', '$window', function($http, $q, $injector, $window) {
var user = null;
var getCurrentUser = function() {
var url = "http://localhost:8080/api/currentuser";
var response = $http.post(url, {}).then(function(response) {});
return response;
};
return {
getCurrentUser: getCurrentUser,
}
}]);
app.factory('authInterceptor', ['$rootScope', '$q', '$window', '$injector', 'Ace',
function($rootScope, $q, $window, $injector, Ace) {
return {
request: function(config) {
config.headers = config.headers || {};
if ($window.localStorage.token) {
config.headers.Authorization = 'Bearer ' + $window.sessionStorage.token;
}
return config;
},
response: function(response) {
if (response.status === 401) {
// handle the case where the user is not authenticated
} else {
Ace.getCurrentUser().then(function() {
console.log("Got current user");
});
}
return response || $q.when(response);
}
};
}
]);
app.config(function($httpProvider) {
$httpProvider.interceptors.push('authInterceptor');
});
【问题讨论】:
-
在
authInterceptor中使用var Ace = $injector.get('Ace');之类的注入器注入Ace尝试一次
标签: javascript angularjs interceptor