如何使用 angular 缓存在页面刷新时存储?

How to use angular cache to store on page refresh?

我正在尝试使用 angular-cache v4.2: angular-cache 我无法在页面刷新之间存储它。

我尝试创建一个 codepen,但由于无法在其中注入模块而没有取得太大进展。 codepen.

angular.module('myApp', ['angular-cache'])
  .config(function (CacheFactoryProvider) {
    angular.extend(CacheFactoryProvider.defaults, { maxAge: 15 * 60 * 1000 });
  })

然而,即使我让它创建缓存,它也会在页面刷新时丢失缓存 - 我已经尝试了三种不同的存储类型,但无法在 chrome 上进行任何工作版本 39。

借助 Shehryar 的评论回答。参见:

codepen

 // To add to local storage
  localStorageService.set('localStorageKey','Now this!');
  // Read that value back
  $scope.value = localStorageService.get('localStorageKey');

使用angular 本地存储模块,此示例将存储页面刷新后创建的缓存。也可以加入

localStorageService.clearAll();

然后刷新页面删除初始值。