2010-12-20 47 views
0

我试图设置在模板中的一些内容,只有当所呈现的项目可见的是“当前项目”。您可以基于当前绑定的数据应用可见绑定吗?

这里是到目前为止的代码,我想只能够使内模板的一部分,如果所呈现的数据项有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> 

我该怎么做呢?

回答

1

你要使用{if}{/if}

<script type="text/html" id="objectTemplate"> 
    <article> 
     <h3>${Name}</h3> 
     {if $item.data.id === viewModel.activeObject()} 
     {{html Text}} 
     {/if} 
    </article> 
</script> 
相关问题