如何解决Angular 8 Form Array控件无法进行选择
尝试通过所选国家/地区,使各个州/地区与同级下拉菜单绑定。不幸的是它抛出了一个错误。
代码中有什么问题。金迪建议。
HTML
<form [formGroup]='entityForm'>
<div formArrayName="optionGroups" class="ui-g ui-g-12 ui-g-nopad " >
<div class="ui-g ui-g-12 ui-g-nopad " *ngFor="let item of entityForm.controls['optionGroups'].controls; let $index=index" [formGroupName]="item">
<select formControlName="country" class="ui-inputtext" (change)="onCountryChange(entityForm.controls['optionGroups'].controls[$index].controls['country'].value,$index)" >
<option>--Select--</option>
<option *ngFor="let c of countries" [ngValue]="c.value">{{c.label}}</option>
</select>
<select formControlName="state" class="ui-inputtext">
<option>--Select--</option>
<option *ngFor="let state of states[entityForm.controls['optionGroups'].controls[$index].controls['country'].value]" value="state.value">{{state.label}}</option>
</select>
</div>
</div>
Angular 8-TS
public countries = [];
public states = [];
public entityForm: FormGroup;
constructor() {
this.entityForm = this.fb.group({
optionGroups : this.fb.array([
this.fb.group({
country : [],state : [],}),])
this.countries = [{"label":"United States","value":"US"},{"label":"Canada","value":"CA"}];
this.states=[];
}
onCountryChange(selectedCountry: string,formIndex : number) : void {
this.states[selectedCountry] = this.getStates(selectedCountry)
}
错误在选择国家/地区
解决方法
您尝试使用[formGroupName]='item'
访问数组的formGroups。 item
不是名称,而是对象(FormGroup)。那就是您在控制台中看到的对象。请更改为[formGroup]='item'
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。