[AngularJS] $http Cache

Source: Internet
Author: User

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

/** * Created by Answer1215 on 12/15/2014.*/Angular.module (' App ', [' ngmaterial ']). Controller (' Mainctrl ',function(Bookmarksservice, bookmarkscachefactory) {varMainctrl = 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) {varBookmarksservice = {}; Bookmarksservice.getbookmarks=function() {            return$http. Get ('./data/bookmarks.json '), {cache:bookmarkscachefactory}); }        returnBookmarksservice; }). Factory (' Bookmarkscachefactory ',function($cacheFactory) {return$cacheFactory (' Bookmarks '); });

<!DOCTYPE HTML><HTML><HeadLang= "en">    <MetaCharSet= "UTF-8">    <title></title>    <Linkrel= "stylesheet"href= "//rawgit.com/angular/bower-material/master/angular-material.css">    <Linkrel= "stylesheet"href= "./bower_components/angular-material/themes/indigo-theme.css">    <Linkrel= "stylesheet"href= "./bower_components/angular-material/themes/teal-theme.css"></Head><BodyNg-app= "App"Ng-controller= "Mainctrl as main" >    <md-content>        < SectionLayout= "column"layout-align= "Start Start">            <Divng-repeat= "B in Main.bookmarks">{{B.title}}</Div>        </ Section>    </md-content>    <Md-buttonNg-click= "Main.getbookmarks ()"class= "Md-primary md-raised"Md-theme= "Indigo">Load Bookmarks</Md-button>    <Md-buttonNg-click= "Main.removecache ()"class= "Md-primary md-raised"Md-theme= "Teal">Clear Cache</Md-button>    <!--Angular Material Dependencies -    <Scriptsrc= "//cdn.jsdelivr.net/hammerjs/2.0.4/hammer.js"></Script>    <Scriptsrc= "//ajax.googleapis.com/ajax/libs/angularjs/1.3.6/angular.js"></Script>    <Scriptsrc= "//ajax.googleapis.com/ajax/libs/angularjs/1.3.6/angular-animate.js"></Script>    <Scriptsrc= "//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 ' -    <Scriptsrc= "//rawgit.com/angular/bower-material/master/angular-material.js"></Script>    <Scriptsrc= "App.js"></Script></Body></HTML>

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

After click "Clear Cache" and then'll load data again.

[AngularJS] $http Cache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.