I use this block of code to create my form:
@Input() fetchedTask: Task;
taskForm: FormGroup;
formThresholds: FormArray;
this.taskForm = this._formBuilder.group({
taskId: null,
groupId: this.groupId,
name: ["", [Validators.required]],
taskType: this.taskTypeId,
etc.
configuration: this._formBuilder.group({
name: ["", Validators.required],
path: ["", Validators.required],
thresholds: this._formBuilder.array([])
})
});
I later set values of the form using setValue():
this.taskForm.controls["taskId"].setValue(this.fetchedTask.taskId);
I set the value of my FormArray using:
this.fetchedTask.configuration.thresholds.forEach((x)=>{
this.addItem(x.value, x.name);
})
addItem(value: number, name: string): void {
this.formThresholds = this.taskForm.get('configuration.thresholds') as FormArray;
this.formThresholds.push(this.createItem(value, name));
}
createItem(value: number, name: string): FormGroup{
return this._formBuilder.group({
value: value,
name: name
});
}
I really don't know how to approach looping through my array values and showing them in my form, with populated values.
I tried this but with no success:
<div *ngFor="let threshold of taskForm.get('configuration.thresholds') let i = index;">
<div [formGroupName]="i">
<input formControlName="name" placeholder="Threshold name">
<input formControlName="value" placeholder="Threshold value">
</div>
</div>