AngularJs:如何正确缓存$ HTTP? [英] AngularJs: How to cache $http properly?
问题描述
我已经启用缓存在我的$ HTTP请求和现在成功的回调只运行的第一次。如果这种预期我想知道或有件事情我需要知道的缓存$ HTTP?
I've enabled cache in my $http request and now the callback for success only runs the first time. I'm wondering if this is expected or there's something I need to know about caching $http ?
这是我一直想:
$http.get('/foo/bar', { cache: true })
.success(function(data){
// does foo
})
.error(function(){
// uh oh
});
比方说,我已经处理一次数据,但也有每一次,我想运行其他命令。所以,如果这是真的数据进行缓存或已经上市,我不想再重复自己,但让我们说,如果我打开和关闭的动画元素,应该在哪里去这样?!
Let's say I've processed the data once, but there's also other commands that I'd like to run, every time. So, if it's true the data is cached or already available I don't want to repeat myself, but let's say, If I'm opening and closing elements with animations, where should this go ?!
感谢您看!
推荐答案
您可以使用 $ cacheFactory
对象。请参阅: http://docs.angularjs.org/api/ng.$cacheFactory
You can use the $cacheFactory
object. See : http://docs.angularjs.org/api/ng.$cacheFactory
您可以缓存一样,$ http请求:
You can cache $http request like that :
var $httpDefaultCache = $cacheFactory.get('$http');
如果你想在缓存中检索特定的网址做的:
If you want to retrieve a specific url in cache do :
var cachedData = $httpDefaultCache.get('http://myserver.com/foo/bar/123');
$您可以清除缓存太:
$You can clear the cache too :
$httpDefaultCache.remove('http://myserver.com/foo/bar/123');
或
$httpDefaultCache.removeAll();
完整的帖子在这里: http://pseudobry.com/power-up-%24http。 HTML
这篇关于AngularJs:如何正确缓存$ HTTP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!