我在Angular中使用$ resources api来管理我的restful api,我使用拦截器来捕获responseError,当再次发送相同的$ resources请求时发生错误(实际上只出现超时的时候我会完全拒绝请求之前尝试几次)
所以,这是我的拦截器:
angular.module('myModule').factory('RequestInterceptor', ['$q', '$injector',
function($q, $injector) {
return {
responseError: function(rejection) {
return $injector.get('$http')(rejection.config);
}
};
}
]);
这是我的服务,而我管理的$资源并创建列表方法发送GET请求并使用拦截器:
angular.module('myModule')
.factory('ApiResources', ['$resource', 'RequestInterceptor',
function($resource, interceptor) {
var url = 'http://www.myapi.com/schedules',
schedules = $resource(url, {}, {
list: {
method: 'GET',
interceptor: interceptor
}
});
return {
schedules: schedules
};
}
]);
现在在模块运行方法:
.run(['ApiResources',
function(myapi) {
myapi.schedules.list();
}
确定,所以,在执行列表()时,请求sended和在myapi,与错误产生的响应,例如500,所以responseError执行和其他请求被发送到myapi由于这一行:
return $injector.get('$http')(rejection.config);
当然,这里我使用$ http和因为我使用$资源为我的拦截未在$ httpProvider推,所以,我需要再次发送请求,但不直接使用$ http,我需要再次使用$资源。有一个简单的方法(使用任何拒绝对象作为配置)再次使用$资源做到这一点?