【问题标题】:knockout.js - use a for binding within a withknockout.js - 在 with 中使用 for 绑定
【发布时间】:2016-06-22 18:06:25
【问题描述】:

我想做一些类似下面的 html 的事情,我在桌子上使用 with binding,但在桌子内我想做 foreach loop 并访问 foreach 的每个元素。

当我这样做时,th 标签不起作用,只有当我从 html 中删除 with binding 时它才会起作用。

我怎样才能让它工作(即使用值 1、2、3 呈现表头)?

<table data-bind="with: myData">
    <tr>
        <th><input type="checkbox" value=""></th>
        <!-- ko foreach: [1,2,3] -->
        <th data-bind="html: $data"></th>
        <!-- /ko -->
    </tr>
    <!-- other stuff here -->
</table>

【问题讨论】:

  • 多一点线索会有所帮助。 myData 包含什么?它是可观察的吗?
  • 它是可观察的,但可能是空的。如果不为空,则包含 JSON 对象

标签: javascript knockout.js


【解决方案1】:

如果myData 不是observableis empty,则可能会发生这种情况,而没有为其分配任何值。

我已经尝试过您的代码here,它工作正常。

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签