Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Templates 使用foreach的knockout.js可重用模板_Templates_Foreach_Knockout.js - Fatal编程技术网

Templates 使用foreach的knockout.js可重用模板

Templates 使用foreach的knockout.js可重用模板,templates,foreach,knockout.js,Templates,Foreach,Knockout.js,我有这个模板 <!--ko template: { name: 'multiCheckBtn', data: { elems: posGenders, compareWith: gender, switch: switchCheckBtn} }--> <!--/ko--> <script id="multiCheckBtn" type="text/html"> <span class="fakeCheck" data-bind="foreach:

我有这个模板

<!--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>

//这里$data变成了t的别名,我不知道如何访问其他参数,比如$data.switch
我想要的是在
foreach
中访问
compareWith
switch
最初传递给模板的变量,但我只能在
foreach
之前访问它们。内部循环
$data
变量成为
t
的别名,我无法访问其他变量


是否有一种方法可以将数据传递到
foreach
循环,以便像在中尝试访问一样访问它?

您可以使用
$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>


这是一个有用的工具:

我可以向您保证,我尝试了很多变体,甚至是
$parent.$data.switch
,但从未尝试过您编写的变体。谢谢