问题
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