带缓存处理的两种写法

过程:点击button触发load()方法,请求数据成后显示到页面中。如果已经请求过则从缓存中读取。

在线浏览

写法1:

    function demo(){
if (demo.cache == undefined) {
return $http.get('https://api.github.com/users/github')
.then(function(data, status, headers){
demo.cache = data.data;
return $q(function (resolve, reject) {
resolve(demo.cache);
});
})
}else {
console.log('from cache');
return $q(function (resolve, reject) {
resolve(demo.cache);
});
}
} // 点击加载
$scope.load = function() {
demo().then(function(data){
$scope.list = data.data;
})
}

写法2:

感觉第二种写法好些,注意细节。

    function demo(){
let deferred = $q.defer(), that = this;if (that.cache == undefined) {
$http.get('https://api.github.com/users/github')
.success(function(data, status, headers){
that.cache = data;
deferred.resolve(that.cache);
})
}else {
console.log('from cache');
deferred.resolve(that.cache);
}
return deferred.promise;
} // 点击加载
$scope.load = function() {
demo().then(function(data){
$scope.list = data;
})
}

写法3 利用闭包缓存结果

    // 利用闭包缓存结果
function demo2() {
let defer = $q.defer(), cache;
return function() {
if (cache == undefined) {
$http.get('https://api.github.com/users/github')
.then((res) => {
cache = res.data;
defer.resolve(cache);
})
}else {
console.log('from cache');
defer.resolve(cache);
}
return defer.promise;
}
} // 点击加载
let startDemo = demo2();
$scope.load = function() {
startDemo().then(function(data){
$scope.list = data;
})
}

angularJs中缓存数据,免去重复发起请求的几种写法的更多相关文章

  1. 不要在Android的Application对象中缓存数据!

    前言   在你的App中的很多地方都需要使用到数据信息,它可能是一个session token,一次费时计算的结果等等,通常为了避免Activity之间传递数据的开销,会将这些数据通过持久化来存储. ...

  2. 不要在Application中缓存数据

    在你的App中的很多地方都需要使用到数据信息,它可能是一个session token,一次费时计算的结果等等,通常为了避免Activity之间传递数据的开销,会将这些数据通过持久化来存储.   有人建 ...

  3. AngularJS中实现显示或隐藏动画效果的3种方式

    本篇体验在AngularJS中实现在"显示/隐藏"这2种状态切换间添加动画效果. 通过CSS方式实现显示/隐藏动画效果 思路: →npm install angular-anima ...

  4. ajax相同url和参数,将不会重复发起请求

    IE浏览器下使用GET发送请求时,如果两次请求的地址和参数相同,在不刷新页面的情况下,浏览器会缓存第一次请求的内容,服务端更新后浏览器仍然显示第一次的内容. 解决办法: 一. GET请求URL后加随机 ...

  5. 关于scrapy中如何区分是接着发起请求还是开始保存文件

    一.区分 根据yield迭代器生成的对象是request对象还是item对象 二.item 1.配置tem对象 在items.py文件中设置类 class MyscrapyItem(scrapy.It ...

  6. java中key-value数据有重复KEY如何存储

    http://www.iteye.com/problems/87219 Map<Key, List<Value>>, 这个好 师兄厉害,给介绍了个神器:guava

  7. C#将数据集DataSet中的数据导出到EXCEL文件的几种方法

    using System; using System.Collections.Generic; using System.Text; using System.Data; using System.W ...

  8. ASP.NET服务器控件OnClientClick事件中Eval()作为js方法的参数的一种写法

    参考代码: <input type="button" OnClientClick='<%#Eval("DeptID", "DelUserD ...

  9. 从map中取出最大或最小value对应的key---多种写法

    package com.yuwanlong.hashing; import java.util.ArrayList; import java.util.Collections; import java ...

随机推荐

  1. Oracle 18c新特性一览

    1. 一般新特性 1.1. Shadow Lost Write Protection Shadow lost write protection检测到一个丢失的写,它会导致一个主要的数据损坏.可以在不需 ...

  2. September 30th 2017 Week 39th Saturday

    The simplest answer is often the correct one. 最简单的答案通常是最正确的答案. Simplest is always best. Sometimes yo ...

  3. here i am(歌手BryanAdams的歌曲)

    here i am(歌手BryanAdams的歌曲) 编辑 目录 1歌曲信息 2中英文歌词 1歌曲信息编辑 1. 歌手:Bryan Adams 布莱恩·亚当斯 生日:1959年11月5日 星座:天蝎座 ...

  4. fun() 的 拆分和 for 遍历 的结合---------> 函数容器

    fun() 的 拆分和 for 遍历 的结合--------->  函数容器

  5. php解决约瑟夫环的问题

    php里面解决约瑟夫环还是比较方面的,但是下面的方法太费空间 <?php class SelectKing{ private $m;//幅度 private $n;//总数 public fun ...

  6. Echarts 曲线数少于图例数解决方法

    在上一篇文章 Echarts 多曲线“断点”问题解决方法 中说到了Angular 项目中要使用 Echarts 的方法. 说明了自己解决当“每一条曲线的横坐标不相同”时,在各条曲线上,它们的值采用数组 ...

  7. acl 4 year statistics

  8. tyvj1953 Normal

    题目链接 正解:点分治+$FFT$. 很想吐槽一下$bzoj$,为什么搬了别的$oj$的题还设成权限题.. 首先我们考虑期望的线性性,即考虑每个点的贡献. 显然每个点的贡献就是它在点分树上的深度,所以 ...

  9. BZ4326 运输计划

    Time Limit: 30 Sec Memory Limit: 128 MB Submit: 2132 Solved: 1372 Description 公元 2044 年,人类进入了宇宙纪元.L ...

  10. 随手练——博弈论入门 leetcode - 486. Predict the Winner

    题目链接:https://leetcode.com/problems/predict-the-winner/ 1.暴力递归 当前数组左边界:i,右边界:j: 对于先发者来说,他能取到的最大值是:max ...