我应该在哪里使用$ templateCache?

时间:2016-02-24 08:28:35

标签: angularjs angular-templatecache

我想使用$ 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?

由于

2 个答案:

答案 0 :(得分:2)

run是使用$templateCache的正确位置。由于$templateCache是一项服务,因此在配置阶段无法访问,因为尚未创建它。 config用于使用其提供程序配置服务(如$templateCache)。您只能将提供者注入config

答案 1 :(得分:0)

我的观点是,在大多数情况下,您不应该直接编写直接访问$templateCache的代码。您应该做的是使用gulp等构建系统和gulp-angular-templatecache等插件。

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

因此,您上面提供的代码将成为:

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

并且$templateCache.run()内的使用被推送到自动生成的代码,而这些代码是您从未见过的。

相关问题