我一直在尝试从interceptorService(file:authenticationServices.js)调用UserService.openLogin()函数,但我没有成功。
一切正常,但是当我尝试将UserService作为依赖项注入时,在interceptorService中,AngularJS会返回以下错误:
“未捕获错误:[$ injector:cdep]找到循环依赖关系:$ http< - UserService< - interceptorService< - $ http< - $ templateRequest< - $ compile”。
拜托,有人可以帮助我吗?
file:authenticationModule.js
angular
.module('app.authentication', [
'app.authentication.controllers',
'app.authentication.services',
'app.authentication.directives']
)
.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when('/register', {
templateUrl: 'app/modules/authentication/authenticationViews/authenticationUserCreate.html',
controller: 'authenticationController as loginCtrl',
css: 'app/modules/authentication/authenticationViews/css/authenticationStyles.css',
})
.when('/edit', {
templateUrl: 'app/modules/authentication/authenticationViews/authenticationProfileEdit.html',
controller: 'authenticationController as loginCtrl',
css: 'app/modules/authentication/authenticationViews/css/authenticationStyles.css',
})
}])
.config(['$httpProvider', function ($httpProvider) {
$httpProvider.interceptors.push('interceptorService');
}])
file:authenticationControllers.js
angular.module('app.authentication.controllers', [])
.controller('authenticationController', ['UserService', '$location', '$uibModalInstance',
function (UserService, $location, $uibModalInstance) {
var self = this;
self.user = {
username: '',
password: ''
};
self.login = function () {
UserService.login(self.user).then(
function (success) {
if (success.status == '400') {
window.alert(success.data.error_description);
}
else {
$location.path('/jobs');
$uibModalInstance.close();
}
},
function (error) {
UserService.logout();
window.alert(error.message)
})
};
self.closeLogin = function () {
$uibModalInstance.dismiss();
}
self.logout = function () {
UserService.logout();
};
}])
file:authenticationServices.js
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;
}
}
}])
答案 0 :(得分:0)
我找到了一种方法来注入UserService而没有循环依赖性错误。但我不知道是否正确。有人知道吗?
查看临时解决方案:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
PlaceList.Combo_list = comboBox1;
}
}