【问题标题】:Where should I use $templateCache?我应该在哪里使用 $templateCache?
【发布时间】:2016-02-24 08:28:34
【问题描述】:

我想使用 $templateCache 服务将一些 html 添加到缓存中。在 Angular 文档示例中,它只显示了在运行中使用它的情况:

var myApp = angular.module('myApp', []);
myApp.run(function($templateCache) {
  $templateCache.put('templateId.html', 'This is the content of the template');
});

但是,当我尝试通过 myApp 配置添加一些模板时,似乎存在注入器错误。有没有办法使用不在 RUN 中的 templateCache?

谢谢

【问题讨论】:

  • 您是否 100% 肯定您不能在配置中注入 $templateCache?什么是喷油器错误?
  • 它的错误:$injector:modulerr 模块错误
  • @mostruash $templateCache 是一项服务,不能注入到配置中。您只能注入提供程序。
  • @hansmaad,所以只能将工厂注入到配置中?
  • @uksz 提供者。为什么要在config 中添加模板? run 怎么了?

标签: angularjs angular-templatecache


【解决方案1】:

run 是使用$templateCache 的正确位置。由于$templateCache 是一个服务,它在配置阶段是不可访问的,因为它还没有被创建。 config 用于使用它们的提供者配置服务(如$templateCache)。您只能将提供程序注入config

【讨论】:

    【解决方案2】:

    我的意见是,大多数时候,您根本不应该编写直接访问$templateCache 的代码。您应该做的是使用诸如gulp 之类的构建系统和诸如gulp-angular-templatecache 之类的插件。

    那么您的模板只是一堆.html 文件,您的编辑器将它们识别为html,并在您编辑时进行适当的linting,您所要做的就是确保您的应用声明对模板模块的依赖。

    所以你上面给出的代码会变成:

    var myApp = angular.module('myApp', ['myappTemplates']);
    

    并且在.run() 中使用$templateCache 会被下推为自动生成的代码,而您永远不会看到。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-01
      • 1970-01-01
      • 2011-06-02
      • 2019-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多