【发布时间】:2020-08-20 02:11:11
【问题描述】:
我在组件中配置了以下 formGroup:
constructor(private apiService: ApiService) {
this.form = new FormGroup({
product: new FormControl(),
shops: new FormGroup({})
});
}
当我从选择栏中选择供应商时,我想将其所有商店加载为复选框。 在加载复选框的函数中,我填写了表单组:
loadShops(product: string): void {
this.apiService.getShopsThatSellProduct(product).subscribe({
next: data => {
this.shops = data;
const checkboxes: FormGroup = this.form.get('shops') as FormGroup;
checkboxes.reset();
this.shops.forEach((shop, index) => {
checkboxes.addControl('shop_' + index, new FormControl(shop));
});
}
,
error: err => {
console.log(err.message);
}
});
}
在我的 html 中,我使用了以下代码:
<div class="shops">
<mat-form-field>
<mat-select placeholder="Shops" >
<mat-option formGroupName="shops" *ngFor="let someFormControl of form.get('shops').value | keyvalue">
<mat-checkbox [formControlName]="someFormControl.key"> {{someFormControl.value.name}}</mat-checkbox>
</mat-option>
</mat-select>
</mat-form-field>
</div>
每个商店都有以下字段:
{
name: string
owner: string
address : string
}
选择产品后,商店列表会更新。但是,默认情况下会选择所有商店,当我尝试选择/取消选择任何商店选项时,值会变为未定义。
为什么{{someFormControl.value.name}} 在每个选项的第一个“取消选择”之后变得未定义?
【问题讨论】:
标签: angular