angularjs: how to add caching to resource object?

后端 未结 8 684
栀梦
栀梦 2020-12-02 08:08

To add caching inside http is pretty straight forward. ( by passing cache=true )

http://docs.angularjs.org/api/ng.$http has Cache option.

How do I add simila

8条回答
  •  感动是毒
    2020-12-02 08:58

    I'm using angular-resource 1.5.5 and set my code up the following way:

    Summary

    Set action as query, and since the "query" action is expecting a response deserialized as an array, isArray will need to be explicitly set to true. My understanding is by default ngResource actions expect objects except query. See here

    Controller

    angular.module("app")
        .controller('myCtrl',['$scope','myService',function($scope, myService) {
            $scope.myData= myService.myResource.query();
        }]);
    

    Service

    angular.module('app')
        .factory('myService',['$resource',function($resource){
            var baseUrl = 'http://www.MyExample.com/';
            return{
                myResource:$resource(baseURL + '/myEndpoint/:id', {id:'@id'},{
                    'query':{
                        method:'GET',
                        cache:'true',
                        isArray:true
                    }}),
            }
        }]);
    

提交回复
热议问题