【问题标题】:Knockout js if in Durandal如果在 Durandal 中淘汰 js
【发布时间】:2014-04-18 17:14:55
【问题描述】:

对于我的生活,我无法弄清楚为什么这不起作用。我正在尝试将每两种产品包装在一个 div 中。第一个产品效果很好,但第二个没有绑定数据

<div  class="prods" data-bind="foreach: products">
    <!-- ko if: $index()%2 == 0 -->
        <div class="prodset">
            <div class="paneProduct">
                <div class="title" data-bind="html: product_title"></div>
                <div data-bind="html: $index()"></div>
            </div>
    <!-- /ko -->
    <!-- ko if: $index()%2 == 1 -->
            <div class="paneProduct">
                <div class="title" data-bind="html: product_title"></div>
                <div data-bind="html: $index()"></div>
            </div>
        </div>
    <!-- /ko -->
</div>

知道我哪里出错了

【问题讨论】:

标签: javascript knockout.js durandal


【解决方案1】:

一个问题是&lt;!-- ko --&gt;&lt;div class="prodset"&gt; 的开始和结束标记的位置,因为它目前的方式不会使用打开和关闭div 标记呈现正确的HTML

http://jsfiddle.net/gonefishern/ALBGQ/

【讨论】:

    【解决方案2】:

    cmets/answers 已经告诉你哪里出错了 - @michael-best has explained this well 另一个问题。

    要实现您想要做的事情,您最好将数据分成对,然后将这些对绑定到您的视图。如果每个单独的产品都具有相同的 HTML,那么您最好还是使用模板绑定。 Here's a JSBin that shows one way to achieve this.

    【讨论】:

      猜你喜欢
      • 2017-11-03
      • 2013-04-09
      • 2014-08-01
      • 2015-02-24
      • 1970-01-01
      • 2015-02-24
      • 2013-10-13
      • 2015-04-10
      • 1970-01-01
      相关资源
      最近更新 更多