Javascript AngularJS中的循环依赖
我一直试图从interceptorService(文件:authenticationServices.js)调用UserService.openLogin()函数,但没有成功 一切正常,但当我尝试将UserService作为依赖项注入interceptorService时,AngularJS在interceptorService中返回以下错误:Javascript AngularJS中的循环依赖,javascript,angularjs,Javascript,Angularjs,我一直试图从interceptorService(文件:authenticationServices.js)调用UserService.openLogin()函数,但没有成功 一切正常,但当我尝试将UserService作为依赖项注入interceptorService时,AngularJS在interceptorService中返回以下错误: “未捕获错误:[$injector:cdep]找到循环依赖项:$http我找到了一种方法,可以在没有循环依赖项错误的情况下注入UserService。但
“未捕获错误:[$injector:cdep]找到循环依赖项:$http我找到了一种方法,可以在没有循环依赖项错误的情况下注入UserService。但是我不知道是否正确。有人知道吗?” 看看临时解决方案:
.factory('interceptorService', ['StorageService', '$injector', function (StorageService, $injector) {
return {
request: function (config) {
var userData = StorageService.userData;
if (userData.token) {
config.headers = config.headers || {};
config.headers.Authorization = 'Bearer ' + userData.token;
}
return config;
},
responseError: function (rejection) {
debugger;
switch (rejection.status) {
case 401:
$injector.get('UserService').openLogin();
break;
case 400:
window.alert("Erro: " + rejection.status + "Descrição: " + rejection.data.error_description);
break;
default:
window.alert("Erro: " + rejection.status);
}
return rejection;
}
}
}])
我找到了一种注入UserService的方法,没有循环依赖错误。但我不知道是否正确。有人知道吗 看看临时解决方案:
.factory('interceptorService', ['StorageService', '$injector', function (StorageService, $injector) {
return {
request: function (config) {
var userData = StorageService.userData;
if (userData.token) {
config.headers = config.headers || {};
config.headers.Authorization = 'Bearer ' + userData.token;
}
return config;
},
responseError: function (rejection) {
debugger;
switch (rejection.status) {
case 401:
$injector.get('UserService').openLogin();
break;
case 400:
window.alert("Erro: " + rejection.status + "Descrição: " + rejection.data.error_description);
break;
default:
window.alert("Erro: " + rejection.status);
}
return rejection;
}
}
}])
这是一个循环引用,因为您的服务依赖于$http,并且您已经配置了一个依赖于您的服务的拦截器。http->interceptor->your service->http。可能相关:您正在将拦截器服务推送到httpprovider中,然后将httpprovider注入拦截器服务。这是一个循环引用,因为您的se服务依赖于$http,并且您已经配置了一个依赖于您的服务的拦截器。http->interceptor->your service->http。可能相关:您正在将拦截器服务推送到httpprovider中,然后将httpprovider注入拦截器服务
angular.module('app.authentication.services', [])
.factory('StorageService', [function () {
return {
isAuth: false,
userData: {
userName: "",
token: ""
},
}
}])
.factory('UserService', ['$http', '$uibModal', 'StorageService', function ($http, $uibModal, StorageService) {
return {
login: function (user) {
var data = "grant_type=password&username=" + user.userName + "&password=" + user.password;
var serviceBase = "http://localhost:53944/";
return $http.post(serviceBase + 'token', data, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } })
.then(function (response) {
if (response.data.access_token) {
StorageService.isAuth = true;
StorageService.userData.token = response.data.access_token;
}
return response;
});
},
openLogin: function () {
$uibModal.open({
animation: true,
templateUrl: 'app/modules/authentication/authenticationViews/authenticationLogin.html',
controller: 'authenticationController',
controllerAs: 'loginCtrl',
size: 'md'
})
},
logout: function () {
StorageService.userData.token = "";
StorageService.userData.userName = "";
StorageService.isAuth = false;
}
};
}])
.factory('interceptorService', ['StorageService', 'UserService', function (StorageService, UserService) {
return {
request: function (config) {
var userData = StorageService.userData;
if (userData.token) {
config.headers = config.headers || {};
config.headers.Authorization = 'Bearer ' + userData.token;
}
return config;
},
responseError: function (rejection) {
debugger;
switch (rejection.status) {
case 401:
UserService.openLogin();
//window.alert("Erro: " + rejection.status);
break;
case 400:
window.alert("Erro: " + rejection.status + "Descrição: " + rejection.data.error_description);
break;
default:
window.alert("Erro: " + rejection.status);
}
return rejection;
}
}
}])
.factory('interceptorService', ['StorageService', '$injector', function (StorageService, $injector) {
return {
request: function (config) {
var userData = StorageService.userData;
if (userData.token) {
config.headers = config.headers || {};
config.headers.Authorization = 'Bearer ' + userData.token;
}
return config;
},
responseError: function (rejection) {
debugger;
switch (rejection.status) {
case 401:
$injector.get('UserService').openLogin();
break;
case 400:
window.alert("Erro: " + rejection.status + "Descrição: " + rejection.data.error_description);
break;
default:
window.alert("Erro: " + rejection.status);
}
return rejection;
}
}
}])