【问题标题】:Best way to manually clear out $templateCache in AngularJS在 AngularJS 中手动清除 $templateCache 的最佳方法
【发布时间】:2015-10-10 22:39:21
【问题描述】:

我正在开发一个系统,该系统在整个系统中都使用了大量的 html 模板文件。

问题是当我部署更新时,用户无法看到 html 更改,因为他们的浏览器正在使用它们的缓存版本。到目前为止,我能让用户看到 html 更新的唯一方法是让他们重新加载浏览器。

出于显而易见的原因,这并不理想。随着 mre 用户迁移到该系统,确保每个人都执行该操作已成为一项乏味的工作

有没有一种方法可以强制访问系统的浏览器在我的命令下清除模板缓存?登录系统手动清除吧?

【问题讨论】:

  • $templateCache 不负责页面加载之间的缓存。从服务器端解决它或发出时间戳/版本化请求,例如'template.html?v=123'.

标签: angularjs caching browser-cache


【解决方案1】:

$templateCache.removeAll()好去掉模板缓存

示例代码

myApp.run(function($rootScope, $templateCache) {
   $rootScope.$on('$viewContentLoaded', function() {
      $templateCache.removeAll();
   });
});

【讨论】:

  • 您可以使用任何其他事件来代替viewContentLoaded,这只是示例:)
  • 问题解决了浏览器缓存问题,$templateCache 与问题无关,尽管标题说了什么。
  • @estus ,他说,他用了lot of html template files ?您是否完整阅读了这些问题?
  • 通过添加上面的代码,它似乎按预期工作。但是现在我添加的模块没有加载模板文件(错误:[$compile:tpload] Failed to load template: template/pagination/pagination.html)
【解决方案2】:

您可以使用 angular-cache-buster 插件。我也遇到了加载缓存模板的问题,使用这个很棒的插件解决了这个问题。

https://github.com/saintmac/angular-cache-buster

【讨论】:

    猜你喜欢
    • 2010-11-13
    • 2014-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 2010-12-15
    相关资源
    最近更新 更多