1、Angular中的$cacheFactory的作用: 用於產生一個用來儲存緩衝對象的服務,並且提供對對象的訪問。
2、Angular中的$cacheFactory的方法:
(1)put(key,value);
在緩衝對象中插入一個索引值對(key,value)。
(2)get(key);
在緩衝對象中通過指定key擷取對應的值。
(3)romove(key);
在緩衝對象中通過指定key刪除對應的值。
(4)removeAll();
刪除緩衝對象中所有的索引值對。
(5)destroy();
銷毀這個緩衝對象。
(6)info();
擷取緩衝對象資訊(id,size)。
注意:key:string類型,緩衝對象中的值名稱。
value:所有類型,緩衝對象中的值。
3、Angular中的$cacheFactory的用法:
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>Cache例子</title> <script src="http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js"></script></head><body ng-app="app"><div ng-controller="myCtrl1" ></div><div ng-controller="myCtrl2" ></div><div></div><script type="text/javascript"> var app=angular.module("app",[]); app.controller('myCtrl1',['$scope','$cacheFactory',function($scope,$cacheFactory){ var cache = $cacheFactory('cache01'); cache.put('name','張三'); cache.put('age',18); var info = cache.info(); console.log(info); }]); app.controller('myCtrl2',['$scope','$cacheFactory',function($scope,$cacheFactory){ var cache = $cacheFactory.get('cache01'); var name = cache.get('name'); console.log(name); }]);</script>結果: