在ngFor中引用外部组件

时间:2018-04-03 23:19:33

标签: javascript angular angular5

作为一个例子,我有一个带有两个公共属性的基本组件(为简洁起见,我已经从示例中省略了外部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>

我想在ngFor循环中的label数组中的i索引处添加一个类{/ 1}}元素。

sentimentValues

这会产生错误<label [ngClass]="sentimentValues[i]">...</label>

如何访问ngFor循环中的外部组件值? (或者我如何重新考虑这一点,以便可能不需要_co.sentimentValues is undefined数组?

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

1 个答案:

答案 0 :(得分:1)

编辑:

您的阵列初始化不正确,您需要使用=

public sentimentValues = ['terrible', 'neutral', 'good'];
相关问题