Angular中的$cacheFactory的作用和用法

來源:互聯網
上載者:User
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>
結果:




 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.