【发布时间】:2019-09-15 08:32:29
【问题描述】:
我有一个带有下拉菜单的反应式表单。我在表单中显示用户选择的下拉项目。值来自 API。有几个下拉菜单正在生成,每个下拉菜单中都有一个空选项(空白项目)。但从 API 方面来看,并没有出现空白项。这是我尝试过的。有人可以帮忙解决这个问题吗
查看。
<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>
这就是如何获取项目并将其添加到响应式表单
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],
})
}
【问题讨论】:
-
我遇到了完全相同的问题。我可以从 API 加载值并修补默认选定值,但列表底部包含一个空白的“null”值。
-
如果有 some
<option>似乎可以工作 - 在我的 *ngFor 之前添加<option disabled>Choose</option>不会产生错误。 (角度 8.2.12)
标签: angular angular-reactive-forms reactive-forms