【发布时间】:2016-01-26 01:14:45
【问题描述】:
我正在尝试使用$templateCache 并拥有以下代码:
我所有的模板都编译在templates.js:
angular.module("gm.templates", []).run(["$templateCache", function($templateCache) {$templateCache.put("/app/app-mc3.html","<div class=.......
我的主要app.js 像这样加载它
angular.module('app', ['ui.router', 'ct.ui.router.extras', 'gm.templates',.....
我在加载app.js 之前加载了templates.js 文件,但是当我尝试在我的一项服务中使用$templateCache 时,它已定义但为空,这意味着尚未加载任何模板。
这是我的应用程序中$templateCache.info() 的一些日志。
ClientService-0.1.5.js:2 Object {id: "templates", size: 0}
ClientService-0.1.5.js:27 /app/app-mc3.html
ClientService-0.1.5.js:28 Object {} // console.log($templateCache)
ClientService-0.1.5.js:29 undefined // Trying to find a specific template in the $templateCache
ClientService-0.1.5.js:2 Object {id: "templates", size: 72}
app-0.1.5.js:3 Object {id: "templates", size: 72}
所以,我的理解是模板可用但一开始不可用。看起来是依赖顺序问题。
角度子模块的module.run() 是否在我的应用程序的主入口点之前执行?我感觉注入是正确完成的,但是 gm.templates run() 方法以某种方式对我的服务异步完成。如何在我需要之前强制执行 submodule.run()?
【问题讨论】:
-
请张贴使用“/app/app-mc3.html”的代码。 $templateCache 是同步的,
submodule.run在module.run之前执行。 -
@estus 我在服务中使用它。这是要点gist.github.com/Vadorequest/39b66554769d34a68b50
-
@estus 确实,
submodule.run在module.run之前执行,但submodule.run在module.config之前没有执行,所以所有 gm.templates 都在之后填充我的应用已启动。
标签: javascript angularjs angular-templatecache