如何缓存在angularjs一个HTTP GET服务 [英] How to cache an http get service in angularjs

查看:99
本文介绍了如何缓存在angularjs一个HTTP GET服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够创建一个获取HTTP GET的情况下TS数据对象的要求是空的,填充成功的数据对象的定制服务。下一次服务调用时,设备将不会调用HTTP GET,而是将present数据对象。

I want to be able to create a custom service that fetches an http get request in the case ts data object is empty and populate the data object on success. The next time a service call is made the device will not call the http get and instead will present the data object.

任何想法怎么办呢?

推荐答案

角的 $ HTTP 有一个缓存建于刚刚成立缓存真正在其选项:

Angular's $http has a cache built in. Just set cache to true in its options:

$http.get(url, { cache: true}).success(...);

$http({ cache: true, url: url, method: 'GET'}).success(...);

也可以实现它自己使用 $ cacheFactory (使用$资源时,尤其是handly):

Or you can implement it yourself using $cacheFactory (especially handly when using $resource):

var cache = $cacheFactory('myCache');

var data = cache.get(someKey);

if (!data) {
   $http.get(url).success(function(result) {
      data = result;
      cache.put(someKey, data);
   });

这篇关于如何缓存在angularjs一个HTTP GET服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆