javascript AngularJS 缓存 REST 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15402867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:40:02  来源:igfitidea点击:

AngularJS Caching a REST request

javascriptrestcachingangularjs

提问by catrapture

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

我是 angularJS 的新手,有一个关于缓存等的问题。

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:

在我的 page1Partial 我有这个:

<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?

当我转到下一页时,然后单击返回复选框已清除,这是因为再次调用了我对 Java 服务的 RESful 调用。如何缓存此响应?

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

从我的控制器来看,这每次都会命中我的 REST Web 服务。

$scope.picks = Pick.query();

My service

我的服务

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

回答by Narretz

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

从 1.1.2 ( commit) 开始,所有 $httpConfig 选项都直接暴露在 $resource 操作对象中:

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

回答by Ajay Beniwal

if you replace $resourcewith $httpthen you can directly use below code

如果替换为$resource$http则可以直接使用下面的代码

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