本文介绍了从外部文件加载基因剔除模板,而无需复杂的引擎?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经找到了加载外部模板的引擎,插件和函数,但是我想知道是否有更简单的方法来做到这一点.像这样吗?
I've found engines, plugins and functions to load external templates, but I'm wondering if there's a simpler way to do it. Something like this?
templates.html
templates.html
<script id="testTemplate" type="text/html">
<div>this is a div</div>
</script>
index.html
index.html
<div id="templateContainer"></div>
<script>
$(document).ready(function() {
$("#templateContainer").load("templates.html");
}
</script>
这项工作有效吗?有任何陷阱"吗?
Will this work? Are there any "gotchas"?
推荐答案
以下是我用来加载包含模板集合的模板文件的方法:
Here's what I use to load a template file that contains a collection of templates:
var loadTemplateCollection = function(file, success) {
$.get('templates/' + file + '.html', function(templates) {
$('body').append('<div style="display:none">' + templates + '<\/div>');
success();
});
};
以下是我使用此功能的示例:
Here's an example where I use this function:
self.loadPage = function () {
if (!self.isLoaded()) {
loadTemplateCollection('uploadwizard', function() {
self.isLoaded(true);
self.uploadWizard();
});
}
}
您的视图应如下所示(if
很重要):
Your view would look something like this (the if
is important):
<div data-bind="template: {'if': isLoaded, name: 'uploadwizard', data: wizard}"></div>
这篇关于从外部文件加载基因剔除模板,而无需复杂的引擎?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!