作为一个例子,我有一个带有两个公共属性的基本组件(对于简洁起见,我从示例中求和了外部FormGroup
。
public sentiment: FormArray;
public sentimentValues: ['terrible', 'neutral', 'good'];
在视图中,我在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>
我想将一个类添加到 label
元素中,该元素位于ngfor loop中的 sentimentValues
数组中的 i
索引。
<label [ngClass]="sentimentValues[i]">...</label>
这会产生错误 _co.sentimentValues is undefined
如何访问NGFOR循环中的外部组件值?(或我如何重新系数,以便不需要sentimentValues
数组?
(附加但相关,输入的[value]
也应为sentimentValues[i]
的值(
编辑:
您的数组初始化不正确,您需要使用=
:
public sentimentValues = ['terrible', 'neutral', 'good'];