如何访问嵌套formGroup中的表单属性?这可能听起来很混乱,但我认为代码会帮助你们理解。 这是formGroup,你可以看到formGroup中有一个数组:
this.formDadosBancarios = this._fb.group({
id: [''],
dados_titular: this._fb.array([
this.initTitular()
])
})
initTitular(){
return this._fb.group({
titular: ['', [<any>Validators.required, <any>Validators.minLength(3)]],
cnpj: [''],
cpf: ['', Validators.required],
data_nasc: ['', Validators.required],
agencia: ['', <any>Validators.required ],
banco: ['', <any>Validators.required],
conta: ['', <any>Validators.required],
tipo: ['', <any>Validators.required],
pessoa_juridica: ['']
})
}
这里是我尝试访问“natTitular”里面的属性cpf
的代码段代码(我知道这段代码看起来很废话,但只是为了说明逻辑):
const cpfCtrl: AbstractControl = this.formDadosBancarios.get('dados_titular').get('cpf');
答案 0 :(得分:2)
您只需使用FormArray
at(index: number) : AbstractControl
方法即可
let array = <FormArray> this.formDadosBancarios.get('dados_titular');
let cpf = array.at(0).get("cpf");
我认为你也可以这样做(但我不确定FormArray&#39; s [0]
)。请参阅docs
this.formDadosBancarios.get('dados_titular[0].cpf'); //maybe it's ".0" instead of "[0]"
this.formDadosBancarios.get(['dados_titular',0,'cpf']);