在角度形式数组中使用 setControl 或 patchValue 时出错 [英] Error when using setControl or patchValue in angular form array

查看:21
本文介绍了在角度形式数组中使用 setControl 或 patchValue 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Please assist, I have nested form array, se below :

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

this._buildFundingItems()如下

        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 || []));

执行上述操作或 this.form.setControl('funding', this.formBuilder.array(data.funding || [])); 我收到一个错误:找不到控制路径:'资金 ->0 ->金额'无法找到控制路径:'funding ->0 ->项'.

doing the above or this.form.setControl('funding', this.formBuilder.array(data.funding || [])); i getting an error : Cannot find control with path: 'funding -> 0 -> amount' and Cannot find control with path: 'funding -> 0 -> items'.

在我执行以下操作之前,我没有收到任何错误,但是在更新表单或(在 valuechanges 上)时,formarray 没有更新.

Before i did the following below, i was not receiving any errors but when updating the form or (on valuechanges), the formarray was not updating.

     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)

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

I saw the following link Angular 4 patchValue based on index in FormArray that's why i tried the first approach.

推荐答案

如果我们不知道您拥有的数据或您想要获得的数据,这将是非常困难的帮助.我认为你的数据就像

It's difficut help if we don't know the data you have or the data you want to get. I supouse your data was like

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

}

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

why not use a function to create the form with the data included?

    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) 创建一个空表单

You can see that you call the function createForm sending data: this.createForm(data), create the form with the date. If you call the function sending null: this.createForm(null) create a empy form

这篇关于在角度形式数组中使用 setControl 或 patchValue 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆