Angular.js has built-in support for caching. Just set cache
to true when passing the $http options:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$http.get(url, { cache: true}).success(...); |
If you want more control you can use the $cacheFactory:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cache = $cacheFactory('sampleCache'); | |
var data = cache.get('sampleKey'); | |
if (!data) { | |
$http.get(url).success(function(result) { | |
data = result; | |
cache.put('sampleKey', data); | |
}); | |
} |