简体   繁体   English

在嵌套的表单组名称中使用父表单组的控件

[英]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

  1. 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>
  2. 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.

相关问题 formGroupName 必须与父 formGroup 指令一起使用 - formGroupName must be used with a parent formGroup directive 如何在formGroup内部的formGroupName之间切换内容 - How to switch content between formGroupName inside formGroup 未嵌套 object 时使用 `formGroupName` - Using `formGroupName` when not nested object 使用 formGroupName 指令的父子组件 - Parent and Child component using formGroupName directive 可重用子组件中的 Angular Reactive Forms:FormArrays 的问题:`formGroupName` 必须与父 formGroup 指令一起使用 - Angular Reactive Forms in Reusable Child Components: Problem with FormArrays: `formGroupName` must be used with a parent formGroup directive formArray里面的formGroup里面的验证控件 - Validation control inside formGroup inside formArray 如何将嵌套的 formGroupName 发送到连接到控制值访问器的输入组件? - How can i send my nested formGroupName to my input component connected to control value accessor? 访问嵌套FormGroup中的FormArray,angular6 - Accessing FormArray inside nested FormGroup , angular6 如何访问FormArray内的嵌套FormGroup的formControlName - How to access the formControlName of a nested FormGroup inside a FormArray formArray 中的 formGroupName 不起作用,出现错误 -&gt; 无法找到带有路径的控件:&#39;fields -&gt; 0 -&gt; unit -&gt; conversion_needed&#39; - formGroupName inside formArray dont work, getting error -> Cannot find control with path: 'fields -> 0 -> unit -> conversion_needed'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM