Knockout.js 在嵌套循环中获取父级的$index
如何访问嵌套循环中的父级索引 例如:Knockout.js 在嵌套循环中获取父级的$index,knockout.js,nested-loops,Knockout.js,Nested Loops,如何访问嵌套循环中的父级索引 例如: 候选索引: 单元索引: 行索引: 我试着这样捆绑: css : {selected : $root.isSelected($parentContext.$parentContext.$index(), $parentContext.$index(), $index())} 我遇到了: 类型错误:$parentContext.$parentContext.$index不是函数 如果要显示父级索引,则需要$parentContext的$parentCont
候选索引:
单元索引:
行索引:
我试着这样捆绑:
css : {selected : $root.isSelected($parentContext.$parentContext.$index(), $parentContext.$index(), $index())}
我遇到了:
类型错误:$parentContext.$parentContext.$index
不是函数
如果要显示父级索引,则需要
$parentContext
的$parentContext
,因此需要编写:
Row index: <span data-bind="text: $parentContext.$parentContext.$index()"></span>
演示。这是正确的
$parents[1]
可以很好地获取祖父母$data,但这是获取绑定上下文的方法。我想在函数中使用它,但出现了以下错误:TypeError:$parentContext.$parentContext.$index不是函数我已经完成绑定:css:{selected:$root.isSelected($parentContext.$parentContext.$index(),$parentContext.$index(),$index())}当我更改第一个参数时,它工作正常。但是如果没有,我有这个类型错误,你在哪里有这个css绑定?在里面,在里面。对我来说,它工作得很好。。。我的意思是,在html中,这个css绑定在哪里?因为只有在最里面的foreach中,它才起作用。你能用你的代码更新我的示例工作示例吗?
<div class="loop" data-bind="foreach: rows">
<div class="nested-loop" data-bind="foreach: cells">
<!-- ko foreach: candidates -->
<div class="nested-nested-loop"
data-bind="css : {selected :
$root.isSelected($parentContext.$parentContext.$index(),
$parentContext.$index(), $index())}">
Candidate index: <span data-bind="text: $index()"></span>
Cell index: <span data-bind="text: $parentContext.$index()"></span>
Row index: <span
data-bind="text: $parentContext.$parentContext.$index()"></span>
</div>
<!-- /ko -->
</div>
</div>