无法将初始表单值设置为 FormArray
Cannot set initial form values into FormArray
我有一个反应式表单,在 cancel
上必须再次将初始表单值设置到 formGroup 中。
import { Map } from "immutable";
@Input() data: any;
public ngOnInit() {
if (this.data && this.data.controls) {
this.group = this.fb.group({
isActive: [this.data.isActive],
items: this.fb.array(this.buildFormArray(this.data.controlPerformers)),
});
// Deep copy of the formGroup with ImmutableJs
this.originalFormData = Map(this.group).toJS();
}
}
public buildFormArray(controllers: IControlPerformer[]) {
return controllers.map((ctlr) => {
return this.fb.group({
user: [ctrl.userData],
ctrlName: [ctlr.name, Validators.required],
date: [moment(ctlr.date).toDate(), Validators.required],
});
});
}
public cancel() {
const existingItems = this.group.get("items") as FormArray;
while (existingItems.length) {
existingItems.removeAt(0);
}
// Here the error when trying to set the FormArray value
this.group.setValue(this.originalFormData.value);
}
错误信息:
There are no form controls registered with this array yet. If you're using ngModel, you may want to check next tick (e.g. use setTimeout).
这个 有同样的问题,但我无法修复它。
更新 - 低于 formGroup
的值。它看起来不错并且已正确初始化。
{
"isActive": true,
"items": [
{
"user": "Walter",
"ctrlName": "Orders",
"date": "2018-03-18T23:00:00.000Z"
}
}
如果您从表单数组中删除项目,则需要重新添加它们,因为 setValue
或 patchValue
函数在缺少表单控件时不会创建表单控件,而只是 set/modify现有的表单控件值。所以,只需将新控件添加到空 FormArray
:
public cancel() {
const existingItems = this.group.get("items") as FormArray;
while (existingItems.length) {
existingItems.removeAt(0);
}
// Even adding a new FormGroup to the array, the exception remains.
// existingItems.push(this.fb.group({})););
// Here the error when trying to set the FormArray value
this.group.patchValue(this.originalFormData.value);
this.originalFormData.value.items.forEach(item => {
existingItems.push(this.fb.group(item));
});
}
STACKBLITZ:https://stackblitz.com/edit/angular-rsglab?file=app%2Fhello.component.ts
我有一个反应式表单,在 cancel
上必须再次将初始表单值设置到 formGroup 中。
import { Map } from "immutable";
@Input() data: any;
public ngOnInit() {
if (this.data && this.data.controls) {
this.group = this.fb.group({
isActive: [this.data.isActive],
items: this.fb.array(this.buildFormArray(this.data.controlPerformers)),
});
// Deep copy of the formGroup with ImmutableJs
this.originalFormData = Map(this.group).toJS();
}
}
public buildFormArray(controllers: IControlPerformer[]) {
return controllers.map((ctlr) => {
return this.fb.group({
user: [ctrl.userData],
ctrlName: [ctlr.name, Validators.required],
date: [moment(ctlr.date).toDate(), Validators.required],
});
});
}
public cancel() {
const existingItems = this.group.get("items") as FormArray;
while (existingItems.length) {
existingItems.removeAt(0);
}
// Here the error when trying to set the FormArray value
this.group.setValue(this.originalFormData.value);
}
错误信息:
There are no form controls registered with this array yet. If you're using ngModel, you may want to check next tick (e.g. use setTimeout).
这个
更新 - 低于 formGroup
的值。它看起来不错并且已正确初始化。
{
"isActive": true,
"items": [
{
"user": "Walter",
"ctrlName": "Orders",
"date": "2018-03-18T23:00:00.000Z"
}
}
如果您从表单数组中删除项目,则需要重新添加它们,因为 setValue
或 patchValue
函数在缺少表单控件时不会创建表单控件,而只是 set/modify现有的表单控件值。所以,只需将新控件添加到空 FormArray
:
public cancel() {
const existingItems = this.group.get("items") as FormArray;
while (existingItems.length) {
existingItems.removeAt(0);
}
// Even adding a new FormGroup to the array, the exception remains.
// existingItems.push(this.fb.group({})););
// Here the error when trying to set the FormArray value
this.group.patchValue(this.originalFormData.value);
this.originalFormData.value.items.forEach(item => {
existingItems.push(this.fb.group(item));
});
}
STACKBLITZ:https://stackblitz.com/edit/angular-rsglab?file=app%2Fhello.component.ts