PW *_*Kad 75 javascript knockout.js
为简洁起见而更新
如何在嵌套的Knockout foreach/with bindings中引用$ parents'$ parent?
示例 -
<!-- ko foreach: grandParent -->
<tr>
<!-- ko foreach: $parent.parents --> // <-- Doesn't work
<!-- ko foreach: children -->
<td data-bind="if: favToy().name == $parent.$parent.favToy().name">
<span data-bind="text: favToy().name"></span>
</td>
<!-- /ko -->
<!-- /ko -->
</tr>
<!-- /ko -->
Run Code Online (Sandbox Code Playgroud)
原版的
很抱歉这个令人困惑的问题,但我试图达到二级父级的值,以检查当前上下文中的值(如下所示),只显示一个跨度,如果它匹配$ parent的$ parent的值(呃!)
<!-- ko foreach: grandParent -->
<tr>
<!-- ko foreach: $parent.parents -->
<!-- ko foreach: children -->
<td data-bind="if: favToy().name == $parent.$parent.favToy().name">
<span data-bind="text: favToy().name"></span>
</td>
<!-- /ko -->
<!-- /ko -->
</tr>
<!-- /ko -->
Run Code Online (Sandbox Code Playgroud)
以这种方式做起来会更容易,但从我所看到的这是不可能的,或者我做错了:)
<!-- ko foreach: grandParent -->
<tr>
<!-- ko foreach: $parent.parents -->
<!-- ko foreach: children ? favToy().name == $parent.$parent.favToy().name -->
<td data-bind="text: favToy().name"></td>
<!-- /ko -->
<!-- /ko -->
</tr>
<!-- /ko -->
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
Mat*_*and 130
Use the $parents
array, the grandparent would be $parents[1]
. You may also be able to use $root
if the grandParent
object in your example is the topmost parent.
From the docs:
$parents
This is an array representing all of the parent view models:
$parents[0] is the view model from the parent context (i.e., it’s the same as $parent)
$parents[1] is the view model from the grandparent context
$parents[2] is the view model from the great-grandparent context
… and so on.
$root
这是根上下文中的主视图模型对象,即最顶层的父上下文.它通常是传递给ko.applyBindings的对象.它相当于$ parents [$ parents.length - 1].
归档时间: |
|
查看次数: |
38960 次 |
最近记录: |