如果响应来自缓存,$http
是否有任何指示?
例如,假设我打了这两个电话:
$http.get({url, cache: true).then(function(response, ..., moreData) { alert(moreData.fromCache) }) //alerts false
$http.get({url, cache: true).then(function(response, ..., moreData) { alert(moreData.fromCache) }) //alerts true
如您所见,第一个调用实际上是在执行 ajax 调用,因此它不是来自缓存,第二个调用从缓存中获取数据,因此它会发出 true
最佳答案
我不确定信息是否公开 - 文档提到即使资源被缓存,请求(使用缓存)仍然是异步的,因此它看起来像任何其他请求。我唯一能想到的就是滚动你自己的缓存并使用它来知道何时发生了成功的查找。来自文档:
You can change the default cache to a new object (built with $cacheFactory) by updating the $http.defaults.cache property. All requests who set their cache property to true will now use this cache object.
关于javascript - 我怎么知道 $http 响应是否来自缓存 - Angular,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27814621/