So far, I've written below code
<form [formGroup]="testForm">
<div formArrayName="selects">
<mat-form-field *ngFor="let select of selects.controls; let i = index">
<mat-select [formControlName]="i">
<mat-option *ngFor="let option of select.value.options" [value]="option">{{ option }}</mat-option>
</mat-select>
</mat-form-field>
</div>
</form>
In the component file
testForm: FormGroup;
get selects() {
return this.testForm.get('selects') as FormArray;
}
data = [{
initial: 'one',
options: ['two', 'three', 'four']
}, {
initial: 'a',
options: ['b', 'c', 'd']
}];
constructor(private formBuilder: FormBuilder) { }
ngOnInit() {
this.testForm = this.formBuilder.group({
selects: this.formBuilder.array([]),
});
for (let i = 0; i < this.data.length; i++) {
this.selects.push(new FormControl(this.data[i]));
}
}
But this is not working so far. What I am doing wrong here?
Find the Stackblitz here.
Issue: As you can see in the stackblitz, it is not showing the initial value and if we select the option, then also it is not working and also options are disappearing from the select if we select any option.