我想在我的主页中使用jQuery的get()方法包含一个外部模板,但我有一个问题.
Web浏览器控制台中显示以下消息:
TypeError: Invalid template! Template should be a “string” but “undefined” was given as the first argument for mustache#render(template, view, partials) – (mustache.min.js:1:9011)
的index.html
templates.html
{{ name }} wins {{ calc }} points
我-的script.js
// my-script.js
function myTemplate() {
$.get("tpl/templates.html", function(templates) {
var template = $(templates).filter("#tpl-1").html();
var data = {
name: "Guy",
calc: function () {
return 8 + 2;
}
};
var rendered = Mustache.render(template, data);
$('#target').html(rendered);
});
}
然而,它适用于load()方法:
// my-script.js
function myTemplate() {
$("#target").load("tpl/templates.html #tpl-1", function() {
var template = $("#tpl-1").html();
var data = {
name: "Guy",
calc: function() {
return 8 + 2;
}
};
var rendered = Mustache.render(template, data);
$('#target').html(rendered);
});
}
你有想法运行get()方法吗?