0
我想写一个角度拦截器(我在离子)。 目标是拦截超时输出请求(让我们假设它们具有状态-1),显示一个模式,然后重试,直到连接完成。 拦截器看起来像预期的那样行事,但是当连接恢复时,什么都不会发生。恐怕在$timeout
内部有return $http(rejection.config);
是不正确的。拦截器:超时模式与重试
services.config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push(function($injector, $q, $timeout) {
return {
// we use the incerceptor to assign a timeout property to every http request
request: function (config) {
config.timeout = 5000;
return config;
},
responseError: function(rejection) {
// We assume timeouts have status=-1
var $http = $injector.get("$http");
var $rootScope = $injector.get("$rootScope");
// duration defines how long the modal screen will be open (in seconds)
var duration = 5;
var showModalAndRetry = function(rejection) {
$timeout(angular.noop, 1000).then(function() {
$rootScope.$broadcast("app.somethingWentWrong");
$timeout(angular.noop, duration * 1000).then(function() {
$rootScope.$broadcast("app.closeSomethingWentWrong");
console.log("resending");
console.log(rejection);
return $http(rejection.config);
});
});
};
switch(rejection.status) {
case -1:
return showModalAndRetry(rejection);
}
return $q.reject(rejection);
}
}
});
}]);
感谢详细的解答。得到它了。 – NoIdeaHowToFixThis