在角度形式数组中使用setControl或patchValue时出错



请帮忙,我有嵌套的表单数组,如下所示:

this.form = this.formBuilder.group({
projectTitle: ['', [Validators.required, Validators.maxLength(300)]],
projectDescription: ['', [Validators.required, Validators.maxLength(300)]],
funding: this.formBuilder.array([this._buildFundingItems()]),
});

CCD_ 1如下

private _buildFundingItems(): FormGroup {
return this.formBuilder.group({
items: ['', [Validators.required, Validators.pattern(this.regexValidation.shortWordRegex)]],,
amount: ['', [Validators.required, Validators.pattern(this.regexValidation.amountTypeRegex)]],
});
}

// after reloading the page, i get data from api and I tried setting the value as follows 
this.form.setControl('funding', this.formBuilder.array(data.funding || []));

执行上述操作或CCD_ 2 i得到错误:CCD_ 3和CCD_。

在我做以下操作之前,我没有收到任何错误,但在更新表单或(在值更改时(时,formarray没有更新。

let length: number = data[0].funding.length;
while (length-- > 0) {
fundingSupport.push(this._buildFundingItems()); 
}
this.form.controls['funding'] = this.formBuilder.array([]);                   
this.form.controls['funding'].patchValue(data.funding)

我在FormArray中看到了以下基于索引的链接Angular 4 patchValue,这就是我尝试第一种方法的原因。

如果我们不知道您拥有的数据或您想要获得的数据,这将很难提供帮助。我怀疑你的数据像

{
projecTitle:"title",
projectDescription:"description"
items:[{
items:"item 1",
amount:10
},
{
items:"item 2",
amount:5
}]
}

为什么不使用函数创建包含数据的表单呢?

createForm(data:any)
{
this.form = this.formBuilder.group({
projectTitle: [data?data.projecTitle:'', [Validators.required, Validators.maxLength(300)]],
projectDescription: [data?data.projectDescription:'', [Validators.required, Validators.maxLength(300)]],
funding: //See that I change your formBuilder to return
//an array, this is because when create the array don't enclosed
// by [ ]
this.formBuilder.array(this._buildFundingItems(data?data.items:null)),
});
}
//see that I was to return an array of FormGroup
_buildFundingItems(items:any[]|null):FormGroup[]
{
return items?items.map(x=>{
return this.formBuilder.group({
items: [x.items, [Validators.required, Validators.pattern(this.regexValidation.shortWordRegex)]]
amount: [x.amount, [Validators.required, Validators.pattern(this.regexValidation.amountTypeRegex)]]
}):
this.formBuilder.group({
items: ['', [Validators.required, Validators.pattern(this.regexValidation.shortWordRegex)]],,
amount: ['', [Validators.required, Validators.pattern(this.regexValidation.amountTypeRegex)]],
});
}

您可以看到,您调用函数createForm发送数据:this.createForm(data(,创建带有日期的表单。如果您调用发送null的函数:this.createForm(null(创建一个empy表单

试试这样的东西:

this.form = this.formBuilder.group({
projectTitle: ['', [Validators.required, Validators.maxLength(300)]],
projectDescription: ['', [Validators.required, Validators.maxLength(300)]],
funding: this.formBuilder.array([this._buildFundingItems({ items: null, amount: null })]),
});
_buildFundingItems(data): FormGroup {
if (!data) {
data = {
items: null,
amount: null
}
} 
return this.formBuilder.group({
items: [data.items, [Validators.required, Validators.pattern(this.regexValidation.shortWordRegex)]],
amount: [data.amount, [Validators.required, Validators.pattern(this.regexValidation.amountTypeRegex)]],
})
}

我也遇到了同样的问题。尝试这种方法,为每一行创建formBuilder组:

let employeeFormGroups = team.employees.map(employee => this.formBuilder.group(employee));
let employeeFormArray = this.formBuilder.array(employeeFormGroups);
this.teamForm.setControl('employees', employeeFormArray);

有关更多帮助,您可以参考此链接:https://www.concretepage.com/angular-2/angular-2-4-formbuilder-example

相关内容

  • 没有找到相关文章

最新更新