0

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

谢谢

4

2 回答 2

2

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

于 2016-02-24T08:45:37.030 回答
0

我的观点是,大多数时候,您根本不应该编写$templateCache直接访问的代码。你应该做的是使用一个构建系统gulp和一个插件,比如gulp-angular-templatecache.

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

因此,您在上面给出的代码将变为:

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

并且使用$templateCacheinside.run()被下推到自动生成的代码,你永远看不到。

于 2016-02-24T09:16:32.333 回答