在淘汰赛视图中访问 $parent 的 $parent - 嵌套上下文 [英] Access $parent's $parent in knockout view - nesting context

查看:11
本文介绍了在淘汰赛视图中访问 $parent 的 $parent - 嵌套上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为简洁起见进行了更新

如何在嵌套的 Knockout foreach/with bindings 中引用 $parents 的 $parent?

How can I reference the $parents' $parent in nested Knockout foreach / with bindings?

示例 -

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

原创

对不起,这个令人困惑的问题,但我试图达到二级父级的值,以检查当前上下文中的值(如下所示),如果它与 $parent 的 $parent 值匹配,则仅显示跨度(呃!)

Sorry for the confusing question but I am trying to reach a second level parent's value to check against a value in the current context (like below) to only show a span if it matches a $parent's $parent's value (ugh!)

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

这样做会更容易,但从我读到的内容来看这是不可能的,或者我做错了:)

It would be easier to do it this way but from what I have read this is not possible or I am doing it wrong :)

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

任何帮助将不胜感激.

推荐答案

使用 $parents 数组,祖父母将是 $parents[1].如果示例中的 grandParent 对象是最顶层的父对象,则您也可以使用 $root.

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.

来自文档:

$parents

这是一个代表所有父视图模型的数组:

This is an array representing all of the parent view models:

$parents[0] 是来自父上下文的视图模型(即,它是与 $parent 相同)

$parents[0] is the view model from the parent context (i.e., it’s the same as $parent)

$parents[1] 是祖父上下文中的视图模型

$parents[1] is the view model from the grandparent context

$parents[2] 是来自曾祖父母上下文的视图模型

$parents[2] is the view model from the great-grandparent context

……等等.

$root

这是根上下文中的主要视图模型对象,即最顶层的父上下文.它通常是传递给的对象ko.applyBindings.相当于 $parents[$parents.length - 1].

This is the main view model object in the root context, i.e., the topmost parent context. It’s usually the object that was passed to ko.applyBindings. It is equivalent to $parents[$parents.length - 1].

这篇关于在淘汰赛视图中访问 $parent 的 $parent - 嵌套上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆