質問

レンダリングされているアイテムが「現在のアイテム」である場合にのみ、いくつかのコンテンツをテンプレートに設定しようとしています。

これがこれまでのところコードです。私は、内側のテンプレートの一部をレンダリングしている場合にのみレンダリングできるようにしたいと思います。 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>

どうやってそれをしますか?

役に立ちましたか?

解決

使用したいと思うでしょう {if}{/if}

<script type="text/html" id="objectTemplate">
    <article>
        <h3>${Name}</h3>
        {if $item.data.id === viewModel.activeObject()}
        {{html Text}}
        {/if}
    </article>
</script>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top