I've been working on the code below to create dropdowns, but I'm having trouble getting the selected value and options to show up in the dropdowns. Can you help me figure out what's wrong with the code?
Component code
testForm: FormGroup;
get ctrls() {
return this.testForm.get('ctrls') as FormArray;
}
data = [{
initial: 'hen',
options: ['hen', 'duck', 'parrot']
},
{
initial: 'lion',
options: ['lion', 'tiger', 'cheetah']
}
];
constructor(private router: Router, private formBuilder: FormBuilder) {
this.testForm = this.formBuilder.group({
ctrls: this.formBuilder.array([])
});
for (let i = 0; i < this.data.length; i++) {
this.ctrls.push(this.formBuilder.control(this.data[i]));
}
}
Template code
<form [formGroup]="testForm">
<div formArrayName="ctrls" *ngFor="let animals of ctrls.controls; let i = index">
<mat-form-field>
<mat-select [formControlName]="i" [(value)]="animals.value.initial">
<mat-select-trigger>{{ animals.value.initial }}</mat-select-trigger>
<mat-option *ngFor="let animal of animals.value.options" [value]="animal">{{ animal }}</mat-option>
</mat-select>
</mat-form-field>
</div>
</form>
I would appreciate any guidance on how to fix this issue...
Please click here to visit the stackblitz link and see the problem for yourself.