firstChild
firstChild

Reputation: 356

How to cache http in Angular until parameters changed?

Is it possible to cache some http until parameters used in url change:

app.factory('dataService', function ($http,$rootScope) {
    return {
        getData: function () {
            return $http.get(rest.getData
           + $rootScope.number + "/" + $rootScope.blb
            ).then(function (result) {
                return result.data;
            });
        }
    }
});

So, when $rootScope.number changes in controller, I need to call http again, until then it should be cached. Is it possible and how?

Upvotes: 0

Views: 335

Answers (2)

Rebornix
Rebornix

Reputation: 5270

Angular's $http has cache built in. Set cache as true in your $http request options:

$http.get(url, {cache: true}).then(...);

Upvotes: 1

amitthk
amitthk

Reputation: 1125

If you want to cache data you can do it in a number of ways.

You can cache it inside your service also.

  • Here is post which should help you.

Upvotes: 0

Related Questions