gpt4 book ai didi

templates - 从外部文件加载基因 knockout 模板,而无需复杂的引擎?

转载 作者:行者123 更新时间:2023-12-03 11:37:20 25 4
gpt4 key购买 nike

我已经找到了用于加载外部模板的引擎,插件和函数,但是我想知道是否有更简单的方法来做到这一点。像这样吗

templates.html

<script id="testTemplate" type="text/html">
<div>this is a div</div>
</script>

index.html
<div id="templateContainer"></div>
<script>
$(document).ready(function() {
$("#templateContainer").load("templates.html");
}
</script>

这样行吗?是否有任何“陷阱”?

最佳答案

这是我用来加载包含模板集合的模板文件的方法:

var loadTemplateCollection = function(file, success) {
$.get('templates/' + file + '.html', function(templates) {
$('body').append('<div style="display:none">' + templates + '<\/div>');
success();
});
};

这是我使用此功能的示例:
self.loadPage = function () {
if (!self.isLoaded()) {
loadTemplateCollection('uploadwizard', function() {
self.isLoaded(true);
self.uploadWizard();
});
}
}

您的 View 如下所示( if很重要):
<div data-bind="template: {'if': isLoaded, name: 'uploadwizard', data: wizard}"></div>

关于templates - 从外部文件加载基因 knockout 模板,而无需复杂的引擎?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17073648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com