【发布时间】:2018-05-30 14:43:41
【问题描述】:
我尝试在请求拦截器中使用工厂,但出现循环依赖错误。我不了解 AngularJS 的所有内容,但我知道注入器正在尝试获取依赖于自身的服务。
我的工厂“全局”用于发出一些 HTTP 请求并在屏幕上显示错误消息。
这是我尝试导入工厂的方式:
app.factory("authInterceptor", authInterceptor);
authInterceptor.$inject = ["$q","Global"];
function authInterceptor($q,Global) {
return {
// Add an interceptor for any responses that error.
'responseError': function (response) {
// Check if the error is auth-related.
if (response.status === 401 || response.status === 403) {
Global.show_fail("Connection expired, please authenticate again");
}
return $q.reject(response);
}
};
}
app.config(["$httpProvider",
function ($httpProvider) {
//Registers the interceptor
$httpProvider.interceptors.push("authInterceptor");
}]);
这是 angularjs.org 上的错误
找到循环依赖:$http
这是我在 global.js 中使用的服务:
app.factory('Global', ['$rootScope', '$http','$mdDialog','$timeout', function($rootScope, $http, $mdDialog, $timeout){ ... }
这个错误有什么原因吗?
提前致谢。
编辑:
我已从我的全球工厂中删除了 $http 服务,并将我的请求驱逐到另一个文件中。
现在我明白了
$templateRequest
但我不明白我从来没有调用过这个 $templateRequest 服务。 有什么想法吗?
【问题讨论】: