I'm having a reactive form with dropdowns. I'm showing user's selected dropdown items in the form. Values are getting from API. There are several drop-downs are generating and in every dropdown there's an empty option(blank item). But from the API side ,there are no blank items are coming. This is what I tried. Can someone help to solve this issue
View.
<form [formGroup]="feedbackForm" (ngSubmit)="onSubmit()">
<table class="table">
<thead>
<tr>
<th scope="col">Category</th>
<th scope="col">Option</th>
</tr>
</thead>
<tbody>
<tr formArrayName="arr" *ngFor="let item of feedbackForm.get('arr').controls; let j = index">
<ng-container [formGroupName]="j">
<td>{{item.value.categories}}</td>
<!--Dropdown items are generating from API-->
<td>
<select name="apivalues" formControlName="option">
<option [ngValue]="item.value.categories">{{item.value.categories}}
<option>
</select>
</td>
</ng-container>
</tr>
</tbody>
</table>
This is how items are getting and added to reactive form
getOptionsFromAPI() {
this.apiService.getOptions()
.subscribe(res=> {
this.arr = this.feedbackForm.get('arr') as FormArray;
if (res['data'].length > 0) {
for (let i = 0; i < res['data'].length; i++) {
this.category = "";
this.getOptions= response['data'][i].options;
this.category = response['data'][i].categories
this.arr.push(this.createItems());
}
}
},
error => {
});
}
createItem() {
return this.fb.group({
category: [this.category],
options: [this.getOptions],
})
}
<option>- Adding<option disabled>Choose</option>before my *ngFor doesn't produce the error. (Angular 8.2.12) - Jeppe