Angular JS解决循环依赖
Angular JS solve circular dependency
我尝试在 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
注入 $httpProvider
来定义 $http
的预处理功能,但是 authInterceptor
依赖于 $http
,它会导致循环依赖问题。
要解决此循环依赖问题,您可以使用 $injector
服务连接 Ace
app.factory('authInterceptor', ['$rootScope', '$q', '$window', '$injector',
function($rootScope, $q, $window, $injector) {
return {
response: function(response) {
if (response.status === 401) {
// handle the case where the user is not authenticated
} else {
var Ace = $injector.get('Ace');
Ace.getCurrentUser().then(function() {
console.log("Got current user");
});
}
return response || $q.when(response);
}
};
}
]);
另一个解决方法是在 运行() 块而不是 config() 块中注册拦截器,但请记住,在执行 run()
之前,对 [=11= 的任何调用]与authInterceptor
无关
我尝试在 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
注入 $httpProvider
来定义 $http
的预处理功能,但是 authInterceptor
依赖于 $http
,它会导致循环依赖问题。
要解决此循环依赖问题,您可以使用 $injector
服务连接 Ace
app.factory('authInterceptor', ['$rootScope', '$q', '$window', '$injector',
function($rootScope, $q, $window, $injector) {
return {
response: function(response) {
if (response.status === 401) {
// handle the case where the user is not authenticated
} else {
var Ace = $injector.get('Ace');
Ace.getCurrentUser().then(function() {
console.log("Got current user");
});
}
return response || $q.when(response);
}
};
}
]);
另一个解决方法是在 运行() 块而不是 config() 块中注册拦截器,但请记住,在执行 run()
之前,对 [=11= 的任何调用]与authInterceptor