2010-12-20 62 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> 
相關問題