0

我有这个模板

<!--ko template: { name: 'multiCheckBtn', data: { elems: posGenders, compareWith: gender, switch: switchCheckBtn} }-->
<!--/ko-->

<script id="multiCheckBtn" type="text/html">
  <span class="fakeCheck" data-bind="foreach: { data: $data.elems, as: 't' } ">
    // here $data becomes alias to t and I don't how to access other params, like $data.switch
    <span class="btn" data-bind="text: t, css: { selected: t == $data.compareWith() }, click: $data.switch } "></span>
  </span>
</script>

我想要的是在里面foreach访问compareWithswitch传递给模板的变量,但我只能在之前访问它们foreach。内部循环$data变量成为别名t,我无法访问其他变量。

有没有办法可以将数据传递给foreach循环,以便我可以像尝试访问一样访问它?

4

1 回答 1

1

您可以使用$parent对象访问它们:

<script id="multiCheckBtn" type="text/html">
  <span class="fakeCheck" data-bind="foreach: { data: $data.elems, as: 't' } ">
    <span class="btn" data-bind="text: t, css: { selected: t == $parent.compareWith() }, click: $parent.switch } "></span>
  </span>
</script>

这是工作小提琴:http: //jsfiddle.net/vyshniakov/nVsgK/

于 2013-01-22T11:05:45.840 回答