[AngularJS] $http cache

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   sp   for   java   

By default your HTTP requests with the $https service in Angular are not cached. By setting some options, you can turn caching on.

 

/** * Created by Answer1215 on 12/15/2014. */angular.module(‘app‘, [‘ngMaterial‘])    .controller(‘MainCtrl‘, function(BookmarksService, BookmarksCacheFactory) {        var mainCtrl = this;        mainCtrl.bookmarks = [];        mainCtrl.getBookmarks = function() {            BookmarksService.getBookmarks().then(function(response) {                mainCtrl.bookmarks = response.data;            });        }        mainCtrl.removeCache = function() {            BookmarksCacheFactory.remove(‘./data/bookmarks.json‘);        }    })    .service(‘BookmarksService‘, function($http, BookmarksCacheFactory) {        var BookmarksService = {};        BookmarksService.getBookmarks = function() {            return $http.get(‘./data/bookmarks.json‘, {cache: BookmarksCacheFactory});        }        return BookmarksService;    })    .factory(‘BookmarksCacheFactory‘, function($cacheFactory) {        return $cacheFactory(‘bookmarks‘);    });

 

<!DOCTYPE html><html><head lang="en">    <meta charset="UTF-8">    <title></title>    <link rel="stylesheet" href="//rawgit.com/angular/bower-material/master/angular-material.css">    <link rel="stylesheet" href="./bower_components/angular-material/themes/indigo-theme.css">    <link rel="stylesheet" href="./bower_components/angular-material/themes/teal-theme.css"></head><body ng-app="app" ng-controller="MainCtrl as main" >    <md-content>        <section  layout="column" layout-align="start start">            <div ng-repeat="b in main.bookmarks">{{b.title}}</div>        </section>    </md-content>    <md-button ng-click="main.getBookmarks()" class="md-primary md-raised" md-theme="indigo">Load Bookmarks</md-button>    <md-button ng-click="main.removeCache()" class="md-primary md-raised" md-theme="teal">Clear cache</md-button>    <!-- Angular Material Dependencies -->    <script src="//cdn.jsdelivr.net/hammerjs/2.0.4/hammer.js"></script>    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.6/angular.js"></script>    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.6/angular-animate.js"></script>    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.6/angular-aria.js"></script>    <!-- Angular Material Javascript using RawGit to load directly from `bower-material/master` -->    <script src="//rawgit.com/angular/bower-material/master/angular-material.js"></script>    <script src="app.js"></script></body></html>

 

"Load bookmarks" only work for the first time clicking the button and load the data.

After click "clear cache", then will load data again.

[AngularJS] $http cache

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.