gpt4 book ai didi

javascript - knockout ,嵌套foreach

转载 作者:可可西里 更新时间:2023-11-01 01:36:35 24 4
gpt4 key购买 nike

让我有猫和小猫的人类

class Master
{
String masterName;

Cat[] cats;
}
class Cat
{
String catName;

Kitten[] kittens;
}

class Kitten
{
String kittenName;
}

现在我想在 html 中显示我所有的小猫和猫与主人。我用

                    <!-- ko foreach: humans -->
<!-- ko foreach: cats -->
<!-- ko foreach: kittens -->
<p data-bind="$data.kittenName"></p>
<p data-bind="$parent.catName"></p>
<p data-bind="???????"></p> <!-- How get master's name? -->
<!-- /ko -->
<!-- /ko -->
<!-- /ko -->

最佳答案

来自knockout documentation

$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[1] 访问主视图模型。

关于javascript - knockout ,嵌套foreach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11721270/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com