对于某些http请求,我希望从内存中返回数据,而不是让它访问服务器。我知道我可以编写http拦截器,但我不确定如何实际返回请求的响应?
myModule.factory('myHttpInterceptor', function ($q) {
return {
// optional method
'request': function (config) {
// return my data here [200], and stop the call from going through
return config;
}
};
});
我们在工作中做了什么,我们是否在总是返回承诺的cacheService中封装http请求。总结:'return isCachedObject? $ q.resolve(cacheInstance):从httpRequest返回结果。它可以成为你的选择吗? – 2017-03-16 14:51:34
@ÉricRoberge是的,无线工作,但希望有一个更优雅的解决方案:)也许没有一个! – Chris
另一个选择是缓存httpRequest的承诺。所以接下来对相同请求的调用可以绑定到第一个调用的承诺。 – 2017-03-16 15:01:58