[AngularJS] AngularJS $cacheFactory service

Angular's $cacheFactory can be used on its own or to customize the behavior of $http calls. This lesson introduces the API and shows how to integrate $cacheFactory into your project.

Link

 

angular.module('app', [])
  .controller('AppCtrl', function ($scope, $http, $cacheFactory) {

    $http.defaults.cache = $cacheFactory('myCache', {capacity: 2});
//capacity: 2  only cache two http request, if the third comes, then the first one in cache will be popup.

    $scope.loadPerson = function (num) {
      $http
        .get('//swapi.co/api/people/' + num + '/')
        .then(function (result) {
          console.log(result.data.name);
        });
    };

  });

 

<!DOCTYPE html>
<html ng-app="app">
<head lang="en">
  <meta charset="UTF-8">
  <title></title>
</head>
<body ng-controller="AppCtrl">

<button ng-click="loadPerson(1)">Luke Skywalker</button>
<button ng-click="loadPerson(2)">C-3PO</button>
<button ng-click="loadPerson(3)">R2-D2</button>

<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.min.js"></script>

</body>
</html>

 

posted @ 2015-05-12 01:20  Zhentiw  阅读(321)  评论(0)    收藏  举报