I want to be able to create a custom AngularJS service that makes an HTTP ‘Get’ request when its data object is empty and populates the data object on success.
The next time a call is made to this service, I would like to bypass the overhead of making the HTTP request again and instead return the cached data object.
Is this possible?
Angular’s $http has a cache built in. According to the docs:
Boolean value
So you can set
cacheto true in its options:or, if you prefer the config type of call:
Cache Object
You can also use a cache factory:
You can implement it yourself using $cacheFactory (especially handly when using $resource):