對於某些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