Javascript 带有formGroup more formArray和formGroup的PatchValue或setValue
我有第二张表格。我需要在字段中填入来自银行的日期,一个json文件。在Javascript 带有formGroup more formArray和formGroup的PatchValue或setValue,javascript,angular,typescript,angular6,Javascript,Angular,Typescript,Angular6,我有第二张表格。我需要在字段中填入来自银行的日期,一个json文件。在populateFields方法中,我得到一个参数,其中包含将要填充的数据。但是,它只填充值对于itens,数组中的描述,它不会执行 错误: 错误错误:未捕获(承诺中):TypeError:\u this.form.controls.itens.value[i]。description.patchValue不是函数 我尝试使用以下表达式: this.form.controls.itens.value[i].descript
populateFields
方法中,我得到一个参数,其中包含将要填充的数据。但是,它只填充值对于itens
,数组中的描述,它不会执行
错误:
错误错误:未捕获(承诺中):TypeError:\u this.form.controls.itens.value[i]。description.patchValue不是函数
我尝试使用以下表达式:
this.form.controls.itens.value[i].description.patchValue(item.description);
但是,我得到了上面提到的错误
generateFormGroup(): void {
this.form = this._formBuilder.group({
id: [null],
alias: ['Relatório de Despesa', [Validators.required, Validators.maxLength(50)]],
job: [null, [Validators.required]],
customer: [{ value: null, disabled: true }, [Validators.required]],
contact: [{ value: null, disabled: true }, [Validators.required]],
currency: [{ value: 1, disabled: true }, [Validators.required]],
exchangeRate: [{ value: null, disabled: true }],
advanceValue: ['0'],
itens: this._formBuilder.array([this.createItem()]),
});
}
createItem(): any {
return this._formBuilder.group({
id: [null],
product: [null, [Validators.required]],
productIcon: [null],
description: this._formBuilder.group({
descriptionProduct: [null],
origin: [null],
km: [null],
destination: [null],
rental: [null],
days: [null],
reason: [null],
taglist: [null]
}),
date: [this.today, [Validators.required]],
value: [0, [Validators.required, Validators.min(0.01)]],
currency: [{ value: 1, disabled: true }, [Validators.required]],
currencyAlias: [this.currency],
receipt: [null],
isLoading: [false],
receiptId: [null],
receiptExtension: [null],
});
}
populateFields(data: any): void {
data.itens.forEach((item, i) => {
this.form.controls.itens.value [i] .description.patchValue (item.description) // error
}
this.itens = data.itens;
this.form.controls.itens.patchValue(data.itens);
}
this.form['controls']['itens']['controls'][index]['controls'].description.patchValue(item.description)代码>
更新:
我使用方括号表示访问属性来抑制错误,例如类型“AbstractControl”上不存在属性“controls”
你可以在网上阅读更多关于它的信息
只有当您有多个级别的FormControl时,这才是一个问题。如果您有直接的内容,可以使用
表示法,甚至是.get()
方法来访问控件
this.form.controls.someControl.patchValue(someValue)
this.form.get('someControl').patchValue(someValue)
上面两个做同样的事情。如果它是一个简单的表单数组,那么可以使用.at()
方法
结合这些元素并进行一些类型转换,您可以这样做
((this.form.get('controls') as FormArray).at(index) as FormGroup).get('description').patchValue(item.description);
从理论上讲,上述方法应该有效:)
this.form.controls.itens.value[i]
将为您提供一个formGroup
。您需要访问this.form.controls.itens.value[i].controls['description'].patchValue()
您能解释一下您使用这种方法的原因吗?