AngularJs: How to cache $http properly?

谁说胖子不能爱 提交于 2019-12-11 23:35:55

问题


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 ?

This is what I've been trying:

$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 ?!

Thanks for looking!


回答1:


You can use the $cacheFactory object. See : http://docs.angularjs.org/api/ng.$cacheFactory

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');

or :

$httpDefaultCache.removeAll();

Complete post here : http://pseudobry.com/power-up-%24http.html




回答2:


I'm pretty sure that the new GET request isn't running so no corresponding success callbacks are called.



来源:https://stackoverflow.com/questions/18963766/angularjs-how-to-cache-http-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!