我有401错误的responseError处理程序。 HTTP拦截器将要求模态用户进行身份验证并继续重试HTTP请求。但问题是,如果我有多个失败的请求拦截器向我显示多个模态,我需要进行几次验证。在第一次请求中进行身份验证后,如何重试所有HTTP请求?
$httpProvider.interceptors.push(function($q, $injector, Auth) {
return {
responseError: function(rejection) {
if (rejection.status === 401) {
return Auth.authenticate().then(function() {
return $injector.get('$http')(rejection.config);
});
}
return $q.reject(rejection);
}
};
});
.factory('Auth', function($injector) {
return {
authenticate: function() {
var $uibModal = $injector.get('$uibModal');
var modal = $uibModal.open({
templateUrl: '/authenticateModal.html',
controller: 'AuthenticateModalController',
});
return modal.result.then(function() {
// success
});
}
}
});
.controller('AuthenticateModalController', function ($scope, $uibModalInstance, $http, Auth) {
$scope.submit = function() {
Auth.login({
'email': $scope.email,
'password': $scope.password
}).then(function successCallback(response) {
$uibModalInstance.close();
}, function errorCallback(response) {
console.log('error');
});
};
})
答案 0 :(得分:0)
如果您只需要1个模态用于多次调用Auth
工厂,则应定义它以便每次都可以访问相同的模态实例:
.factory('Auth', function($injector) {
var $uibModal = $injector.get('$uibModal');
var modal
return {
authenticate: function() {
if (!modal) {
modal = $uibModal.open({
templateUrl: '/authenticateModal.html',
controller: 'AuthenticateModalController',
});
}
return modal.result.then(function() {
// success
});
}
}
});