如何在 Angular 中推送到表单数组([])
2019-08-06
8559
我有一个从 API 获取数据的动态复选框,以下是
html
文件
<form [formGroup]="form" (ngSubmit)="submit()">
<label formArrayName="summons" *ngFor="let order of form.controls.summons.controls; let i = index">
<input type="checkbox" [formControlName]="i">
{{summons[i].name}}
</label>
<div *ngIf="!form.valid">At least one order must be selected</div>
<br>
<button [disabled]="!form.valid">submit</button>
</form>
以下是 typescript 文件:
interface Item {
offenceType4: string;
permSpeed: string;
actSpeed: string;
itemAttributes: ItemAttributes;
offenceLoc: string;
itemNo: string;
summonDate: string;
distCode: string;
summonType: string;
hbtlOffndr: string;
itemAmount: number;
itemAttributesCount: number;
summonAmt: string;
offenceType1: string;
offenceCode1: string;
offenceType2: string;
offenceCode2: string;
offenceType3: string;
category: string;
offenceCode3: string;
offenceCode4: string;
respCode: string;
}
interface ItemAttributes {
attribute5: string;
attribute4: string;
attribute7: string;
attribute6: string;
attribute1: string;
attribute3: string;
attribute2: string;
}
interface RootObject {
items: Item[];
status: Status;
additionalProperties: AdditionalProperties;
metadata: Metadata;
}
export class InquiryResponseMultiselectComponent implements OnInit {
form: FormGroup;
summons = [];
data: any[];
constructor(
private formBuilder: FormBuilder,
private inquiryService: InquiryService
) {
this.form = this.formBuilder.group({
summons: new FormArray([], minSelectedCheckboxes(1)),
});
this.getSummon().subscribe(summons => {
this.summons = summons;
this.addCheckboxes();
});
}
ngOnInit() {
this.getSummon();
}
getSummon() {
return this.inquiryService.getData().pipe(map((item: RootObject) =>
item.items)) ;
}
addCheckboxes() {
this.summons.map(i => {
const control = new FormControl();
const formArray = this.form.controls.summons as FormArray;
formArray.push(control);
});
}
我从 API 获得对动态复选框的响应,然后,我需要将新控件推送到
summons FormArray
,但我收到错误
core.js:9110 ERROR TypeError: Cannot read property 'map' of undefined
如果我不进行类似这样的映射:
addCheckboxes() {
const control = new FormControl();
const formArray = this.form.controls.summons as FormArray;
formArray.push(control);
}
我将获得默认的
formControl
长度等于 1,这是我不想要的,需要建议和指导我如何解决这个问题,
2个回答
由于您使用的是 formbuilder,因此无需使用 FormArray 初始化 formarray,而是使用 formbuilder api
this.form = this.formBuilder.group({
summons: this.formBuilder.array([]),
});
Chellappan வ
2019-08-06
在这里,您在接收数据之前初始化表单,因此您尝试更改已经存在的内容,这比从头开始创建要困难得多。
为此,您可以在订阅
内部
初始化表单,您知道数组应包含哪些元素。
constructor(
private formBuilder: FormBuilder,
private inquiryService: InquiryService
) {
this.inquiryService.getData().subscribe((receivedSummons: RootObject[]) => {
this.summons = receivedSummons[0].data;
let formControls = this.summons.items.map((summon, index) => {
return new FormControl(index === 0);
});
this.form = this.formBuilder.group({
summons: new FormArray(formControls),
});
this.addCheckboxes();
});
}
这样做会使您在 html 表单的开头添加一个条件 (
*ngIf
),以避免在尝试访问尚未初始化的表单时出现错误:
<form *ngIf="form" [formGroup]="form" (ngSubmit)="submit()">
<label formArrayName="summons" *ngFor="let order of form.controls.summons.controls; let i = index">
<input type="checkbox" [formControlName]="i">
{{summons[i].name}}
</label>
<div *ngIf="!form.valid">At least one order must be selected</div>
<br>
<button [disabled]="!form.valid">submit</button>
</form>
Random
2019-08-06