gpt4 book ai didi

javascript - 引用 ngFor 中的外部组件

转载 作者:行者123 更新时间:2023-12-03 02:09:29 25 4
gpt4 key购买 nike

作为示例,我有一个具有两个公共(public)属性的基本组件(为了简洁起见,我在示例中省略了外部 FormGroup

public sentiment: FormArray;
public sentimentValues: ['terrible', 'neutral', 'good'];

在 View 中,我正在迭代 sentiment 数组中包含的控件。

<div *ngFor="let option of sentiment.controls; index as i;">
<label [for]="'sentiment_' + i" class="sentimentLabel">
<input class="sentiment"
[id]="'sentiment_' + i"
type="checkbox"
name="sentiment"
[formControl]="option"
value="option1">
</label>
</div>

我想向 ngFor 循环内 sentimentValues 数组中 i 索引处的 label 元素添加一个类.

<label [ngClass]="sentimentValues[i]">...</label>

这会产生错误_co.sentimentValues未定义

如何访问 ngFor 循环中的外部组件值? (或者我如何重构它,以便也许不需要 sentimentValues 数组?

(附加但相关,输入的[value]也应该是sentimentValues[i]的值)

最佳答案

编辑:

您的数组初始化不正确,您需要使用=:

public sentimentValues = ['terrible', 'neutral', 'good'];

关于javascript - 引用 ngFor 中的外部组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49640448/

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