AngularJS Caching a REST request

喜欢而已 提交于 2020-01-10 19:42:31

问题


I'm new to angularJS and have a question about caching etc.

I have a wizard with two steps, I want to be able to click back and next and have the forms still filled out as the user had them.

In my page1Partial i have this:

<li ng-repeat="pick in picks | orderBy:orderProperty">
<b><span ng-bind="pick.name"/></b>
<input type="checkbox" ng-model="pick.checked" ng-click="updateBasket(pick)">
</li>

When i go to the next page, then click back the checkboxs are cleared, and its because my RESful call to a java service is called again. How can I cache this response?

From my controller, this hits my REST web service every time.

$scope.picks = Pick.query();

My service

angular.module('picksService', ['ngResource']).
    factory('Pick', function ($resource) {
        return $resource('rest/picks/:id', {}, {
            'save': {method: 'PUT'}
        });
    });

回答1:


Since 1.1.2 (commit), all the $httpConfig options are directly exposed in $resource action objects:

return {
  Things: $resource('url/to/:thing', {}, {
    list : {
      method : 'GET',
      cache : true
    }
  })
 };



回答2:


if you replace $resource with $http then you can directly use below code

$http({
    method: 'PUT',
    url: 'url',
    cache:true
});


来源:https://stackoverflow.com/questions/15402867/angularjs-caching-a-rest-request

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