【问题标题】:How to use angular cache to store on page refresh?如何使用角度缓存来存储页面刷新?
【发布时间】:2015-05-18 16:19:19
【问题描述】:

我正在尝试使用 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 上运行。

【问题讨论】:

  • 您可能希望查看local storage 作为数据持久性的一个选项;它也有可用的包装器,比如这个:angular local storage module
  • 谢谢,这是一种享受!

标签: angularjs caching angular-cache


【解决方案1】:

在 Shehryar 的评论的帮助下回答。见:

codepen

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

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

localStorageService.clearAll();

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

【讨论】:

    猜你喜欢
    • 2011-09-24
    • 2019-04-03
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    相关资源
    最近更新 更多