【发布时间】:2015-04-23 16:33:05
【问题描述】:
我正在尝试使用gulp-angular-templatecache 生成模板缓存并将其与 Angular 脚本合并到一个 JS 文件中。
gulp 任务:
gulp.task('generatetemplatecache', function () {
return gulp.src(['dev/app/**/*.tpl.html'])
.pipe(minifyHTML({quotes: true}))
.pipe(templateCache({ module:'templateCache', standalone:true }))
.pipe(gulp.dest('public/app'));
});
这基本上是输出的样子:
var app = angular.module("app",['templateCache']);
angular.module("templateCache", []).run(["$templateCache", function($templateCache) {$templateCache.put("test.tpl.html","<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"UTF-8\"><title>My Title</title></head><body></body></html>");
$templateCache.put("layout/popover.tpl.html","<h3 class=\"popover-title\">Click</h3><div class=\"popover-content\"><table style=\"width:600px\" class=\"table\"><tr ng-repeat=\"customer in customers\"><td>{{customer.name}}</td><td>{{customer.street}}</td></tr></table>ss</div>");
但问题是,我发现它没有从模板缓存加载。每当 Angular 需要模板时,它仍然会抓取原始模板 html 文件。如果该 html 文件不可用,那么将无法显示它。
为什么它不起作用?我做错了什么?模板缓存中的 url 是相对于index.html 还是相对于域的根目录?
【问题讨论】:
-
抓取原始文件是什么意思?这意味着您更改了 HTML 文件,但仍然在浏览器中看到旧的更改?
-
@sma 我的意思是它仍然需要原始文件到位。如果我拿走模板 html 文件,它将无法加载。
-
你能发布你用来创建模板文件的 gulp 任务吗?
-
@sma 我已经发布了生成模板缓存的 gulp 任务。
-
所以这应该是创建一个名为 templates.js 的新文件,对吗?我假设您以某种方式加载此文件,然后将其作为依赖项添加到您的主模块?
标签: javascript angularjs gulp