【发布时间】:2011-05-28 04:55:58
【问题描述】:
我正在尝试将模板中的某些内容设置为仅当正在呈现的项目是“当前项目”时才可见。
这是到目前为止的代码,如果正在渲染的数据项具有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>
我该怎么做呢?
【问题讨论】:
标签: javascript jquery data-binding knockout.js