【问题标题】:$templateCache, ng-template and caching issues - angularjs$templateCache、ng-template 和缓存问题 - angularjs
【发布时间】:2018-05-15 20:37:16
【问题描述】:

我正在尝试缓存我的 angularjs 应用程序的 html 文件,遵循以下建议:https://gist.github.com/ProLoser/6181026

我正在向所有 html 文件(Angular ui bootstrap 使用的模板目录中的文件除外)附加一个动态缓存键,如 ?v=123456790

对于他们自己文件中的部分,即app/views/customer/customer-history.html,这非常有效。但是,对于脚本形式的部分,例如<script type='text/ng-template' id='customer-history'> 找不到模板,请求服务器,导致404。

例如localhost:8080/customer-history/?v=1234567890

我可以看到有问题的模板在 $templateCache 中。使用 ng-template 的只是名称,即 customer-history,而单独的文件是路径 app/views/customer/customer-history.html,如您所料。

这是一个大型应用程序,因此将它们全部移动到自己的文件等中并非易事。我到处搜索,但我没有打开任何东西,所以我猜这是 templateCache 实现方式的基本内容。

我在构建过程中使用 gulp-templatecache 插件,但文档非常有限,我认为问题不存在。

我目前的解决方案是通过将inCache 函数装饰到$templateCache 工厂并在检查期间使用它来附加cacheKey,从而在附加版本号的函数中暂时跳过有问题的模板。但我觉得这有点忽略了重点。

【问题讨论】:

    标签: angularjs angular-templatecache


    【解决方案1】:

    在这里回答我自己的问题。

    因为脚本标签是加载到 $templateCache 中的包含 html 文件的一部分,所以脚本 已经存在。当对 templateUrl 发出请求时,它不应该有缓存清除附录。

    避免这一切的最简单方法是在“templates.js”或任何构建过程的模板输出上设置缓存破坏哈希或字符串。这样整个文件就会失效并根据需要重新加载。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多