【问题标题】:How to set Values to FormArray in Angular 4 after it has been Initialized?初始化后如何在Angular 4中将值设置为FormArray?
【发布时间】:2018-04-29 06:17:37
【问题描述】:

我有一个 formArray,我想在稍后获取数据后为数组设置值,但由于我已经初始化了 formArray,是否可以将初始值设置为 formArray 以及如何设置如果我不得不这样做的价值

constructor(private fb: FormBuilder) {
      this.businessForm = fb.group({
                    businessInfo: this.buildBusinessArray()
                });
        buildBusinessArray() {
                this.businessInfo = this.fb.array([this.buildBusinessGroup()]);
                return this.businessInfo;
            }

            buildBusinessGroup() {
                return this.fb.group({
                    taxNumber: '',
                    legalName: '',
                    `enter code here`dbaName: '',
                    location: '',
                });
            }
    }

这就是我尝试在函数中设置值的方式。但是它没有设置它只是抛出错误的值

this.selected.forEach((selected) => {
    this.businessForm.setValue ({
     'taxNumber': this.merchantInfo[selected.merchantNumber].businessInfo.taxNumber,
    ' legalName': 
     this.merchantInfo[selected.merchantNumber].businessInfo.legalName,
     'dbaName': this.merchantInfo[selected.merchantNumber].businessInfo.dbaName,
     'location': this.merchantInfo[selected.merchantNumber].businessInfo.location
    });
});

我不确定我错过了什么。有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: angular-reactive-forms angular4-forms


    【解决方案1】:

    当您尝试使用setvalue 方法时。您需要设置所有成员。 如果您使用您的代码,请将setValue 替换为patchValue 方法。

    您当前的代码。我已经更新了方法。

    this.selected.forEach((selected) => {
        this.businessForm.patchValue ({
         'taxNumber': this.merchantInfo[selected.merchantNumber].businessInfo.taxNumber,
        ' legalName': 
         this.merchantInfo[selected.merchantNumber].businessInfo.legalName,
         'dbaName': this.merchantInfo[selected.merchantNumber].businessInfo.dbaName,
         'location': this.merchantInfo[selected.merchantNumber].businessInfo.location
        });
    });
    

    您也可以使用setvalue,但您需要初始化businessInfo 属性。

        this.selected.forEach((selected) => {
            this.businessForm.patchValue ({
    businessInfo: '',//assign your value here
             'taxNumber': this.merchantInfo[selected.merchantNumber].businessInfo.taxNumber,
            ' legalName': 
             this.merchantInfo[selected.merchantNumber].businessInfo.legalName,
             'dbaName': this.merchantInfo[selected.merchantNumber].businessInfo.dbaName,
             'location': this.merchantInfo[selected.merchantNumber].businessInfo.location
            });
        });
    

    【讨论】:

    • businesForm 有一个名为 businessInfo 的成员,它是一个 formArray。我不知道如何使用 setValues 对其进行初始化
    • 那你就换个方法。用户 patchValue 都相似,不同之处在于,setvalue 需要所有参数,而 patchvalue 需要您传递的参数
    • 我试过补丁值它没有更新值。 BussinessForm 是 formGroupName,formArrayName 是 businessInfo,所以在这种情况下,补丁值应该应用于 formGroupName 或 formArrayName。
    • 如果补丁值不起作用,那么 setvalue 也不会有用。使用 patchValue 时遇到什么问题
    • 它没有显示任何错误,但它也没有更新值。
    猜你喜欢
    • 2017-04-14
    • 1970-01-01
    • 2018-08-07
    • 2021-09-24
    • 1970-01-01
    • 2019-02-19
    • 1970-01-01
    • 2021-01-15
    • 2020-08-03
    相关资源
    最近更新 更多