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
AngularJS Caching a REST request
提问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
回答by Ajay Beniwal
if you replace $resource
with $http
then you can directly use below code
如果替换为$resource
,$http
则可以直接使用下面的代码
$http({
method: 'PUT',
url: 'url',
cache:true
});