我在我的angularjs应用程序中有一个$http
。
如果我的api返回未经授权的回复,我会显示登录信息。
一旦用户通过模态登录,请求拦截器就会重试$http
请求。
app.factory('LoginInterceptor', function ($injector, $timeout, $q) {
return {
request: function (config) {
return config;
},
response: function (response) {
var LoginModalService = $injector.get('LoginModalService'),
$http = $injector.get('$http'),
$location = $injector.get('$location'),
$rootScope = $injector.get('$rootScope'),
url = response.config.url.split('?')[0];
if (response.data && response.data.token_status <= 0) {
return $timeout(angular.noop, 1000).then(function() {
return LoginModalService.show();
}).then(function() {
return $http(response.config);
}, function() {
$location.path('/login');
return $q.reject(response);
});
}
return response || $q.when(response);
}
}
});
问题是,如果多个请求异步触发登录显示每个请求。
如何停止执行所有其他请求直到用户登录?目前,只有一个请求发生时才能使用。我不想取消所有其他请求,我希望他们一劳永逸地解决用户在签署
你是什么意思的“暂停”?您想要在显示模式时取消所有并行请求,还是希望在用户登录后执行它们? –
用户登录后执行它们。 - 请参阅编辑问题 – CharliePrynn
我写了一个工厂,以便更好地控制。有一个[看](https://www.npmjs.com/package/angular-httpshooter) – Siddharth