访问$ parent的$ parent在淘汰视图

编程入门 行业动态 更新时间:2024-10-19 08:56:27
访问$ parent的$ parent在淘汰视图 - 嵌套上下文(Access $parent's $parent in knockout view - nesting context)

为了简洁而更新

如何引用嵌套的Knockout foreach /绑定中的$父母$ 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 -->

原版的

对于令人困惑的问题抱歉,我试图达到第二级父母的价值,以检查当前上下文中的值(如下),只显示一个跨度,如果它符合$ parent的$ parent值(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 -->

这将是更容易做到这一点,但从我所看到的这是不可能的或我做错了:)

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

任何帮助将不胜感激。

Updated for brevity

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

Example -

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

Original

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 。

从文档 :

$父母

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

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

$ parents [1]是祖父母背景下的视图模型

$ parents [2]是从祖父母的背景来看的模型

… 等等。

$根

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

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

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

更多推荐

本文发布于:2023-07-16 12:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128607.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   parent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!