[英]$injector.get is not a function
I'm trying to make autenticate-interseptor. 我正在尝试做一个autenticate-interseptor。 The core idea is simple: when server-request fails we must login user by http-auth. 核心思想很简单:当服务器请求失败时,我们必须通过http-auth登录用户。 Code look like this: 代码看起来像这样:
(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);
}
};
}
]);
})();
I need this var $http = $injector.get('$http');
我需要这个var $http = $injector.get('$http');
here because of circullar injection error. 这里因为圆圈注射错误。 But when i use that module, i get the error TypeError: $injector.get is not a function
但是当我使用该模块时,我得到错误TypeError: $injector.get is not a function
So, how can i inject $http here? 那么,我怎么能在这里注入$ http?
The order of your injections does not match the strings in the array. 您的注入顺序与数组中的字符串不匹配。
angular
.module('app.authentication')
.factory('AuthenticationInterceptor', ['$q', '$injector', '$location', 'Session',
function AuthenticationInterceptor($location, $q, $injector, Session) {
You are telling angular to inject the $location service as the third argument to the AuthenticationInterceptor
service, but you are using the third argument as if it is the $injector. 你告诉angular注入$ location服务作为AuthenticationInterceptor
服务的第三个参数,但你使用第三个参数就好像它是$ injector。 Change your code to the following: 将您的代码更改为以下内容:
angular
.module('app.authentication')
.factory('AuthenticationInterceptor', ['$location', '$q', '$injector', 'Session',
function AuthenticationInterceptor($location, $q, $injector, Session) {
'$q', '$injector', '$location', 'Session'
$location, $q, $injector, Session
You haven't respected the same order. 你没有尊重同一个订单。
Do yourself a favor, and use ng-annotate to never have this bug again. 帮自己一个忙,并使用ng-annotate再也不会有这个bug。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.