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

查看:115
本文介绍了在knockout视图中访问$ 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的父级值(呃!)

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

我们非常感谢任何帮助。

Any help would be greatly appreciated.

推荐答案

使用 $ 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.

来自 docs


$ 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].

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

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