我正在嘗試使autenticate-interptor。核心思想很簡單:當服務器請求失敗時,我們必須通過http-auth登錄用戶。代碼如下所示:
(function() {
'use strict';
angular
.module('app.authentication')
.factory('AuthenticationInterceptor', ['$q', '$injector', '$location', 'Session',
function AuthenticationInterceptor($location, $q, $injector, Session) {
return {
request: function(config) {
var currentUser = Session.currentUser();
if (!currentUser || !currentUser.id) {
if ($location.path() !== '/login') { $location.path('/login'); }
}
return config;
},
responseError: function(response) {
if (response.status === 401 || response.status === 403) {
var $http = $injector.get('$http');
Session.destroy();
$location.path('/login');
}
return $q.reject(response);
}
};
}
]);
})();
我需要這個var $http = $injector.get('$http');
這裏,因爲circullar注射錯誤的。但是,當我使用該模塊,我得到錯誤TypeError: $injector.get is not a function
那麼,我怎麼可以在這裏注入$ http?
謝謝,此問題修正了錯誤! –
np - 請upvote /接受一個或兩個答案。看起來我們都同時回答,基本上有相同的答案。 –
對不起,我不能接受更多的9分鐘的答案) –