Angularjs $injector.get不是函数
我想做一个自我诱惑的中间人。核心思想很简单:当服务器请求失败时,我们必须通过http身份验证登录用户。代码如下所示:Angularjs $injector.get不是函数,angularjs,Angularjs,我想做一个自我诱惑的中间人。核心思想很简单:当服务器请求失败时,我们必须通过http身份验证登录用户。代码如下所示: (function() { 'use strict'; angular .module('app.authentication') .factory('AuthenticationInterceptor', ['$q', '$injector', '$location', 'Session', function AuthenticationInte
(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”)代码>由于循环注入错误而出现此错误。但是当我使用该模块时,我得到了错误TypeError:$injector.get不是一个函数
那么,我如何在这里注入$http呢
'$q', '$injector', '$location', 'Session'
$location, $q, $injector, Session
你没有遵守同样的命令
帮你自己一个忙,使用ng annotate以避免再次出现此错误。注入顺序与数组中的字符串不匹配
angular
.module('app.authentication')
.factory('AuthenticationInterceptor', ['$q', '$injector', '$location', 'Session',
function AuthenticationInterceptor($location, $q, $injector, Session) {
您告诉angular将$location服务作为第三个参数注入到AuthenticationInterceptor
服务,但您使用第三个参数时,就好像它是$injector一样。将代码更改为以下内容:
angular
.module('app.authentication')
.factory('AuthenticationInterceptor', ['$location', '$q', '$injector', 'Session',
function AuthenticationInterceptor($location, $q, $injector, Session) {
np-请投票/接受一个或两个答案。看起来我们是同时回答的,答案基本上是一样的。对不起,我不能接受答案超过9分钟)