我是angular-http-etag
的作者,所以我只能直接说这个模块的功能。它装饰Angular的$http
服务,允许您指定要缓存的请求。下面是使用的例子,我在readme.md提供:
angular
.module('myApp', [
'http-etag'
])
.config(function (httpEtagProvider) {
httpEtagProvider
.defineCache('persistentCache', {
cacheService: 'localStorage'
})
})
.controller('MyCtrl', function ($http) {
var self = this
$http
.get('/my_data.json', {
etagCache: 'persistentCache'
})
.success(function (data, status, headers, config, itemCache) {
// Modify the data from the server
data._fullName = data.first_name + ' ' + data.last_name
// Update the cache with the modified data
itemCache.set(data)
// Assign to controller property
self.fullName = data._fullName
})
// Synchronous method called if request was previously cached
.cached(function (data, status, headers, config, itemCache) {
self.fullName = data._fullName
})
.error(function (data, status) {
// 304: 'Not Modified'--Etag matched, cached data is fresh
if (status != 304) alert('Request error')
})
})
的需要的服务器端的唯一的事情是确保服务器发送ETag
响应头。你可以在这里找到关于如何在Chrome中检查响应头的信息:https://developers.google.com/web/tools/chrome-devtools/profile/network-performance/resource-loading#view-details-for-a-single-resource
如果你发现你的解决方案,请分享 – garrettmac