可能之前的api写的有些枯燥吧,因为不烧脑,不需要很多逻辑思维来做处理,那么之后的文章会有趣很多,慢慢的开始烧脑了,准备好大量脑细胞的死亡吧~   先来篇简单的缓存服务。

这里野兽把api文档里的$cacheFactory和 $cacheFactory.Cache 放到一起学习。

$cacheFoctory

用于生成一个用来存储缓存对象的服务,并且提供对对象的访问。

$cacheFactory.Cache

一个用于存储和检索数据的缓存对象。主要使用$http和脚本指令来缓存模板和其他数据。

该服务有以下方法:

put(key,value);

在缓存对象中插入一个键值对(key,value)。

get(key);

在缓存对象中通过指定key获取对应的值。

romove(key);

在缓存对象中通过指定key删除对应的值。

removeAll();

删除缓存对象中所有的键值对。

destroy();

销毁这个缓存对象。

info();

获取缓存对象信息(id,size)。

key:string类型,缓存对象中的值名称。

value:所有类型,缓存对象中的值。

使用代码:

  (function () {
angular.module("Demo", [])
.controller("testCtrl", ["$cacheFactory",testCtrl]);
function testCtrl($cacheFactory) {
var myCache = $cacheFactory("my-cache");
myCache.put("cache", "This is cache-content");
myCache.put("another-cache", "This is another cache-content");
var getCache = myCache.get("cache"); //This is cache-content
var getInfo = myCache.info();//{id: "my-cache", size: 2}
myCache.remove("another-cache");
getInfo = myCache.info();//{id: "my-cache", size: 1}
myCache.removeAll();
getInfo = myCache.info();//{id: "my-cache", size: 0}
myCache.destroy();
getInfo = myCache.info();//{size: 0}
};
}());

值的注意的是,这是应用程序的缓存服务,而不是浏览器本地的缓存。所以当你刷新浏览器,初始化整个应用程序的时候,之前的缓存数据都会丢失。那么问 题就来了,怎么才能刷新/初始化应用程序而不丢失之前保存的数据呢,这个可以使用localStorage或者cookies,关于ng的这两个存储操 作,之后的文章会写到,现在根据api一个个慢慢来写,有需要和有兴趣的话,也可以自己网上找资料学习

Angular - - $cacheFactory的更多相关文章

  1. AngularJs $cacheFactory 缓存服务

    可能之前的api写的有些枯燥吧,因为不烧脑,不需要很多逻辑思维来做处理,那么之后的文章会有趣很多,慢慢的开始烧脑了,准备好大量脑细胞的死亡吧~   先来篇简单的缓存服务. 本文将api文档里的$cac ...

  2. angular中$cacheFactory缓存的使用

    最近在学习使用angular,慢慢从jquery ui转型到用ng开发,发现了很多不同点,继续学习吧: 首先创建一个服务,以便在项目中的controller中引用,服务有几种存在形式,factory( ...

  3. Forms in Angular 2

    Input handling is an important part of application development. The ng-model directive provided in A ...

  4. Angular中的$cacheFactory的作用和用法

    1.Angular中的$cacheFactory的作用:    (1)put(key,value); 在缓存对象中插入一个键值对(key,value). (2)get(key); 在缓存对象中通过指定 ...

  5. angularjs1-8,cacheFactory,sce

    <!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...

  6. Event Binding in Angular

    https://www.pluralsight.com/guides/angular-event-binding Introduction In this guide, we will explore ...

  7. angular

  8. Download Excel file with Angular

    源码连接(编写中) 用Angular下载后台返回的Excel文件,用Blob实现,引用FileSaver.js 后台C#代码: [WebMethod] public static byte[] Cal ...

  9. 初识Angular

    一.AngularJs简介 1.AngularJS使用了不同的方法,它尝试去补足HTML本身在构建应用方面的缺陷.AngularJS通过使用我们称为标识符(directives)的结构,让浏览器能够识 ...

随机推荐

  1. 我也谈 AngularJS 怎么使用Directive, Service, Controller

    原文地址:http://sunqianxiang.github.io/angularjs-zen-yao-shi-yong-directiveservicecontroller.html 其转自大漠穷 ...

  2. How to spend you day ?

    如果这是你生命中的最后的一天,你该如何去过好它呢? 不要浪费你生命中的每一分,每一秒!!!

  3. mybatis与spring整合(基于配置文件)

    本文主要介绍了如何将mybatis和spring整合在一起使用,本人使用的是mybatis3.05 + spring3.1.0M2 ,使用dbcp作为数据库连接池. 1.编写数据访问接口(UserDa ...

  4. 使用jquery模拟键盘事件,但window系统并不会真的响应事件,只是浏览器当前页面会响应而已

    <!DOCTYPE html> <html> <head> <title>Demo</title> <meta http-equiv= ...

  5. MyBatis基础用法(一)

    <select id="getErrorTimes" resultType="Integer"> SELECT ErrorTimes FROM `e ...

  6. 一道变态的js题

    一道腾讯js面试题 题目如下: f = function() {return true;}; g = function() {return false;}; (function() { if (g() ...

  7. opencv----(1) mat最好用,和IplImage,cvmat 比较

    学习了几天,发现mat比IplImage,cvmat 好用太多了. 不知道确切的原文出处,我是转自新浪的一篇博文:http://blog.sina.com.cn/s/blog_534497fd0101 ...

  8. 为什么说Neutron不是SDN?

    http://vuejs.com.cn/ 这里面有个canvans 画图的js 代码.有意思,研究一下. Neutron 介绍:https://www.ibm.com/developerworks/c ...

  9. codeforce 611B New Year and Old Property

    暴力搞 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> u ...

  10. Xcode模拟网络状态

    http://www.it165.net/pro/html/201508/50571.html