Angular 主模块依赖于 $templateCache.run() (子模块)
Angular main module depends on $templateCache.run() (submodule)
我正在尝试使用 $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}
所以,我的理解是模板可用,但不是在开始时。看起来这是一个依赖顺序问题。
angular 个子模块的 module.run()
是否在我的应用程序的主入口点之前执行?我感觉注入是正确完成的,但是 gm.templates
运行() 方法以某种方式对我的服务异步完成。如何强制 submodule.运行() 在我需要之前执行?
如果您想在您的应用 运行 之前访问您的模板列表,您可以将它们附加到 window 对象。
您可以通过配置 grunt/gulp 任务来实现。 (gulp 这里有咖啡)
然后您将在 window.gtTemplates
对象中拥有所有模板,并可以在您的服务中使用它们。请注意,$templateCache
也将像以前一样填充。
TEMPLATE_HEADER = 'window.gmTemplates = {};'
TEMPLATE_BODY = 'window.gmTemplates["<%= url %>"] = "<%= contents %>";'
TEMPLATE_FOOTER = 'angular.module("<%= module %>").run(["$templateCache", function($templateCache) {
Object.keys(window.gmTemplates).forEach(function(url) {
$templateCache.put(url, window.gmTemplates[url])
})
}]);'
gulp.task 'template', ->
gulp.src([
'src/**/*.html'
'!src/index.html'
])
.pipe(templateCache(
root: '/'
module: 'app'
templateHeader: TEMPLATE_HEADER
templateBody: TEMPLATE_BODY
templateFooter: TEMPLATE_FOOTER
))
.pipe(gulp.dest('dist/src/'))
我正在尝试使用 $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}
所以,我的理解是模板可用,但不是在开始时。看起来这是一个依赖顺序问题。
angular 个子模块的 module.run()
是否在我的应用程序的主入口点之前执行?我感觉注入是正确完成的,但是 gm.templates
运行() 方法以某种方式对我的服务异步完成。如何强制 submodule.运行() 在我需要之前执行?
如果您想在您的应用 运行 之前访问您的模板列表,您可以将它们附加到 window 对象。
您可以通过配置 grunt/gulp 任务来实现。 (gulp 这里有咖啡)
然后您将在 window.gtTemplates
对象中拥有所有模板,并可以在您的服务中使用它们。请注意,$templateCache
也将像以前一样填充。
TEMPLATE_HEADER = 'window.gmTemplates = {};'
TEMPLATE_BODY = 'window.gmTemplates["<%= url %>"] = "<%= contents %>";'
TEMPLATE_FOOTER = 'angular.module("<%= module %>").run(["$templateCache", function($templateCache) {
Object.keys(window.gmTemplates).forEach(function(url) {
$templateCache.put(url, window.gmTemplates[url])
})
}]);'
gulp.task 'template', ->
gulp.src([
'src/**/*.html'
'!src/index.html'
])
.pipe(templateCache(
root: '/'
module: 'app'
templateHeader: TEMPLATE_HEADER
templateBody: TEMPLATE_BODY
templateFooter: TEMPLATE_FOOTER
))
.pipe(gulp.dest('dist/src/'))