Error: formGroup expects a FormGroup instance - while adding new Component to formArray
Error: formGroup expects a FormGroup instance - while adding new Component to formArray
我正在使用响应式表单方法。我有一个表单(父组件称为:DeductionInvoicesComponent
):
<form [formGroup]="deductionForm">
<div formArrayName="items" class="well well-lg">
<app-deduction-invoice-item
*ngFor="let item of deductionForm.get('items')?.controls; let i=index"
[index]="i"
(removed)="deductionForm.get('items').removeAt($event)">
</app-deduction-invoice-item>
</div>
</form>
<button type="button" class="btn btn-primary" (click)="addItem()">Add an item</button>
父级的 TypeScript 如下:
export class DeductionInvoicesComponent implements OnInit {
deductionForm: FormGroup;
constructor(private fb: FormBuilder) { }
ngOnInit() {
this.deductionForm = this.fb.group({
items: this.fb.array([])
});
}
addItem(){
let control = <FormArray>this.deductionForm.controls.items;
control.push(DeductionInvoiceItemComponent.buildItem());
}
}
表单可以包含多个 DeductionInvoiceItemComponents
作为一个 formArray
。
子组件(名为:DeductionInvoiceItemComponent
的单个项目)如下所示:
<div class="row" [formGroup]="item">
<div class="form-group col-4">
<label class="center-block">Title</label>
<select class="form-control" formControlName="title">
<option value="test">test</option>
</select>
</div>
<div class="form-group col-4">
<label class="center-block">Invoice Number</label>
<input class="form-control" formControlName="invoiceNumber">
</div>
<button (click)="removed.emit(index)" type="button" class="close text-danger" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
表示 formArray 项的单个组件的 TypeScript 如下所示:
export class DeductionInvoiceItemComponent {
@Input()
public index: number;
@Input()
public item: FormGroup;
@Output()
public removed: EventEmitter<number> = new EventEmitter<number>();
static buildItem() {
return new FormGroup({
title: new FormControl('', Validators.required),
invoiceNumber: new FormControl('', Validators.required),
grossAmount: new FormControl('', Validators.required)
});
}
}
当我单击按钮时 addItem()
我收到以下错误消息:
Error: formGroup expects a FormGroup instance
如您所见,我使用名为 buildItem
的静态函数创建了该 FormGroup。我该如何解决这个问题?
在您的 DeductionInvoiceItemComponent 中,您有:
@Input()
public item: FormGroup;
但是您没有将其作为父组件的输入传递。添加它:
<app-deduction-invoice-item
*ngFor="let item of deductionForm.get('items')?.controls; let i=index"
[index]="i"
[item]='item' // <-- HERE
(removed)="deductionForm.get('items').removeAt($event)">
</app-deduction-invoice-item>
我正在使用响应式表单方法。我有一个表单(父组件称为:DeductionInvoicesComponent
):
<form [formGroup]="deductionForm">
<div formArrayName="items" class="well well-lg">
<app-deduction-invoice-item
*ngFor="let item of deductionForm.get('items')?.controls; let i=index"
[index]="i"
(removed)="deductionForm.get('items').removeAt($event)">
</app-deduction-invoice-item>
</div>
</form>
<button type="button" class="btn btn-primary" (click)="addItem()">Add an item</button>
父级的 TypeScript 如下:
export class DeductionInvoicesComponent implements OnInit {
deductionForm: FormGroup;
constructor(private fb: FormBuilder) { }
ngOnInit() {
this.deductionForm = this.fb.group({
items: this.fb.array([])
});
}
addItem(){
let control = <FormArray>this.deductionForm.controls.items;
control.push(DeductionInvoiceItemComponent.buildItem());
}
}
表单可以包含多个 DeductionInvoiceItemComponents
作为一个 formArray
。
子组件(名为:DeductionInvoiceItemComponent
的单个项目)如下所示:
<div class="row" [formGroup]="item">
<div class="form-group col-4">
<label class="center-block">Title</label>
<select class="form-control" formControlName="title">
<option value="test">test</option>
</select>
</div>
<div class="form-group col-4">
<label class="center-block">Invoice Number</label>
<input class="form-control" formControlName="invoiceNumber">
</div>
<button (click)="removed.emit(index)" type="button" class="close text-danger" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
表示 formArray 项的单个组件的 TypeScript 如下所示:
export class DeductionInvoiceItemComponent {
@Input()
public index: number;
@Input()
public item: FormGroup;
@Output()
public removed: EventEmitter<number> = new EventEmitter<number>();
static buildItem() {
return new FormGroup({
title: new FormControl('', Validators.required),
invoiceNumber: new FormControl('', Validators.required),
grossAmount: new FormControl('', Validators.required)
});
}
}
当我单击按钮时 addItem()
我收到以下错误消息:
Error: formGroup expects a FormGroup instance
如您所见,我使用名为 buildItem
的静态函数创建了该 FormGroup。我该如何解决这个问题?
在您的 DeductionInvoiceItemComponent 中,您有:
@Input()
public item: FormGroup;
但是您没有将其作为父组件的输入传递。添加它:
<app-deduction-invoice-item
*ngFor="let item of deductionForm.get('items')?.controls; let i=index"
[index]="i"
[item]='item' // <-- HERE
(removed)="deductionForm.get('items').removeAt($event)">
</app-deduction-invoice-item>