[英]Using a control of parent formgroup inside nested formgroupname
I am creating a form where I am using formgroupname inside a formgroup.我正在创建一个表单,我在表单组中使用 formgroupname。
Html: Html:
<form [formGroup]="parentForm">
<div formGroupName="childGroup">
<input type="text" formControlName="childControl1">
<input type="text" formControlName="parentControl1">
<input type="text" formControlName="childControl2">
</div>
</form>
Ts Code TS代码
this.parentForm = this.formBuilder.group({
parentControl1: new FormControl(),
childGroup: this.formBuilder.group({
childControl1: new FormControl(),
childControl2: new FormControl(),
})
})
I am getting following error.我收到以下错误。
Error : Cannot find control with path: 'childGroup -> parentControl1'错误:找不到带有路径的控件:'childGroup -> parentControl1'
Note: I can't change html structure.注意:我无法更改 html 结构。 Need a workaround of can I achieve this
需要一个解决方法我可以实现这个吗
"parentControl" not belong to ChildGroup “parentControl”不属于 ChildGroup
A workAround is use a getter解决方法是使用吸气剂
get parentControl1():FormControl { return this.parentForm.get('parentControl1') as FormControl }
And use [formControl]
并使用
[formControl]
<div formGroupName="childGroup"> <input type="text" formControlName="childControl1"> <input type="text" [formControl]="parentControl1"> <input type="text" formControlName="childControl2"> </div>
Another workaround is use some like另一种解决方法是使用一些类似
<div formGroupName="childGroup"> <input type="text" formControlName="childControl1"> <input type="text" [ngModel]="this.parentForm.get('parentControl1').value" (ngModelChange)="this.parentForm.get('parentControl1').setValue($event)" [ngModelOptions]="{standalone:true}"> <input type="text" formControlName="childControl2"> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.