如果有人问过这个特定的问题,请原谅。我搜索了博客,搜索了这个,但找不到任何特别符合我需求的东西。
我正在尝试使用 Knockout JS 构建一个单页应用程序,但我对此比较陌生,似乎无法解决这个问题。
我有两个模板:
<script id="template1" type="text/template">
<h3>Template 1</h3>
<button id="templButton" text="Go to template 2" />
</script>
和
<script id="template2" type="text/template">
<h3>Template 2</h3>
</script>
我有两个绑定到这些模板的 div:
<div data-bind="template: { name: template1 }"></div>
<div data-bind="template: { name: template2 }"></div>
在模板 1 中,我有一个按钮,当单击该按钮时,应填充模板 2 并清除模板 1。似乎没有办法做到这一点,没有另一个 3rd 方添加?
更新 1
在页面加载时,我使用 jQuery 获取第一个模板的值:
$(document).ready(function(e) {
$.get("/Controller/Action", function (data) {
// get values from action, convert to array for ko to function
var results = $.parseJSON(data);
var viewModel = {
someBind = ko.mapping.fromJS(results);
}
ko.applyBindings(viewModel);
// now, I thought the best way to do this was to bind to the button here:
$("#templButton").click(function(e) {
// and call the load of the template here,
// however, because I have two templates on the page
// Knockout is trying to bind to both.
});
});
});
没有使用'templateToUse'作为这么多帖子,线程状态,还有另一种方法可以做到这一点吗?我对这一切都很陌生,所以如果我的方法看起来很愚蠢,请原谅。
我已经看到线程指出单击事件应由 Knockout 处理:
<button data-bind="click: function(e) { ... }" />
但这让我回到了最初的问题,如何加载第二个模板,在单击按钮时绑定到它。