javascript AngularJS $ 资源和缓存工厂

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18930857/
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 13:43:28  来源:igfitidea点击:

AngularJS $resource & cache factory

javascriptangularjsangularjs-factory

提问by Yashvit

I have implemented angular $resource with custom functions and parameters as follows:-

我已经使用自定义函数和参数实现了 angular $resource,如下所示:-

.factory('CandidateService', ['$resource', function ($resource) {
    return $resource("api/:action/:id", {},
    {
        'getCandidates': { method: "GET", params: { action: "Candidate" }, isArray: true },
        'getCandidate': { method: 'GET', params: { action: "Candidate", id: "@id" } }
    });
}]);

And I am consuming this in the controller as follows:-

我在控制器中使用它如下:-

.controller('Controller', ['CandidateService', function ($scope, CandidateService) {
  $scope.candidateList = [];

  CandidateService.getAll(function (data) {
    $scope.candidateList = data;   
  });
}]);

This is working absolutely fine. Now I need to cache the data from the api into the CandidateService Factory so it is not loaded eveytime I move between the controllers.

这工作得很好。现在我需要将 api 中的数据缓存到 CandidateService 工厂中,这样我在控制器之间移动时就不会加载它。

So I thought i would do something as follows:-

所以我想我会做以下事情:-

.factory('CandidateService', ['$resource', function ($resource) {
    var Api = $resource("api/:action/:id", {},
    {
        'getCandidates': { method: "GET", params: { action: "Candidate" }, isArray: true },
        'getCandidate': { method: 'GET', params: { action: "Candidate", id: "@id" } }
    });

    var candidateDataLoaded = false;
    var candidateData = [];

    return {
        getCandidates: function () {
            if (!candidateDataLoaded) {
                Api.getAll(function (data) {
                    angular.copy(data, candidateData);
                });
            }
            return candidateData;
        }
    }

}]);

But I just cant get this to work. I think it has something to do with angular factory being a singleton.

但我无法让它发挥作用。我认为这与 angular factory 是单身有关。

Is my approach correct to implement the caching?

我的方法实现缓存是否正确?

回答by Thomas Pons

You can use the $cacheFactory object. See : http://docs.angularjs.org/api/ng.$cacheFactory

您可以使用 $cacheFactory 对象。请参阅:http: //docs.angularjs.org/api/ng.$cacheFactory

You can cache $http request like that :

您可以像这样缓存 $http 请求:

var $httpDefaultCache = $cacheFactory.get('$http');

If you want to retrieve a specific url in cache do :

如果要检索缓存中的特定 url,请执行以下操作:

var cachedData = $httpDefaultCache.get('http://myserver.com/foo/bar/123');

$You can clear the cache too :

$您也可以清除缓存:

$httpDefaultCache.remove('http://myserver.com/foo/bar/123');

or :

或者 :

$httpDefaultCache.removeAll();

Complete post here : Power up $http with caching

在此处完成帖子:使用缓存启动 $http