【问题标题】:Can you apply visible binding based on the data currently being bound?您可以根据当前绑定的数据应用可见绑定吗?
【发布时间】: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


    【解决方案1】:

    你会想要使用{if}{/if}

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-21
      • 1970-01-01
      • 1970-01-01
      • 2014-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多