我正在尝试将模板中的某些内容设置为仅当正在呈现的项目是“当前项目”时才可见。
到目前为止,这是代码,如果正在呈现的数据项具有ID == window.viewModel.activeObject
,我希望只能渲染内部模板的一部分。
<section data-bind='template: { name: "categoryTemplate", foreach: categories }'></section>
<script type="text/html" id="categoryTemplate">
<section>
<h2>${Name}</h2>
<section data-bind='template: { name: "objectTemplate", foreach: Objects }'></section>
</section>
</script>
<script type="text/html" id="objectTemplate">
<article>
<h3>${Name}</h3>
(only render this if the object rendered has ID equal to viewModel.activeObject)
{{html Text}}
</article>
</script>
<script>
$(document).ready(function(){
window.viewModel = { categories : <asp:Literal runat="server" ID="LiteralJSON" />,
activeCategory: ko.observable(0),
activeObject: ko.observable(0)
};
ko.applyBindings(window.viewModel);
});
</script>
我将如何做到这一点?
答案 0 :(得分:1)
您需要使用{if}{/if}
<script type="text/html" id="objectTemplate">
<article>
<h3>${Name}</h3>
{if $item.data.id === viewModel.activeObject()}
{{html Text}}
{/if}
</article>
</script>