我想知道当我在另一个 formBuilder 组中有一个数组时,我必须做什么才能以反应形式使用“setControl”和“get”。例如:
this.formulario = this.formBuilder.group({
title: [this.racPessoa.title, [Validators.required]],
description: [this.racPessoa.description, [Validators.required]],
person: this.formBuilder.group({
idPerson:[this.racPessoa.person.idPerson],
name:[this.racPessoa.person.nome],
personDocument: this.formBuilder.array([])
}),
});
在上面的情况下,如果我想处理“标题,我可以写:
this.formulario.setControl('title', something);
this.formulario.get('title');
但是当我想处理一个人内部的“personDocument”时,我不知道如何写上面的两个句子
我尝试过使用:
this.formulario.setControl('person.personDocument', something);
this.formulario.get('person.personDocument')
但这不起作用。
FormGroup
's setControl
方法不支持嵌套表单控制结构,它只会检测并设置当前层的表单控制,请参阅设置控制 https://github.com/angular/angular/blob/6.0.3/packages/forms/src/model.ts#L1034 and 寄存器控制 https://github.com/angular/angular/blob/6.0.3/packages/forms/src/model.ts#L1005.
对于你的情况,this.formulario.setControl('person.personDocument', something);
只会添加新的表单控件(key of person.personDocument
)到当前图层(您可以在 formGroup 的控件中确认)。
this.formulario = this.formBuilder.group({
title: [this.racPessoa.title, [Validators.required]],
description: [this.racPessoa.description, [Validators.required]],
person: this.formBuilder.group({
idPerson:[this.racPessoa.person.idPerson],
name:[this.racPessoa.person.nome],
personDocument: this.formBuilder.array([])
}),
'person.personDocument': something // newly added form control
});
因此,您需要在确切的层添加表单控件,例如:
(this.formulario.get('person') as FormGroup).setControl('personDocument', new FormControl('aaa'));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)