简体   繁体   English

如何从子组件将“formControlName”添加到角度反应形式“formGroup”?

[英]How to add "formControlName" to angular reactive forms "formGroup" from child components?

I have dynamic components in form and I want to connect fields with angular reactive forms.我在表单中有动态组件,我想将字段与角度反应形式连接起来。

for example we have例如我们有

first-component.html第一个组件.html

<form [formGroup]="myform">
...
   <second-component></second-component>
...
   <button type="submit">
</form>

second-component.html第二个组件.html

<div ngFor ...>
...
   <third-component></third-component>
...
</div>

third-component.html第三组件.html

<div>
...
   <input type="text">
...
</div>

How to add "FormControlName" in third component, that is field?如何在第三个组件即字段中添加“FormControlName”?

You should pass the formGroup as @input to the child components, and then before the html input specify the relative formGroup您应该将 formGroup 作为 @input 传递给子组件,然后在 html 输入之前指定相对的 formGroup

Another method is that you specify the formControl itself, like this:另一种方法是指定 formControl 本身,如下所示:

<input [formControl]="formGroup.get('inputName')" type text>

See this link for more information on the topic: https://angular.io/guide/dynamic-form有关该主题的更多信息,请参阅此链接: https ://angular.io/guide/dynamic-form

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:formControlName 必须与父 formGroup 指令一起使用。 您需要添加一个 formGroup 指令 - Angular 反应式表单 - Error: formControlName must be used with a parent formGroup directive. You'll want to add a formGroup directive - Angular reactive forms Angular 2 反应式表单 - 从 formControlName 管道传递一个值 - Angular 2 reactive forms - piping a value from a formControlName 如何在 angular 反应式 forms 中使用子组件? - How to use child components in angular reactive forms? 可重用子组件中的 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 Angular Reactive 表单表单组 - Angular Reactive forms formgroup 反应形式Angular formGroup - Reactive forms Angular formGroup 反应式表单:如何在 Angular 7/8/9 的稍后时间点将新的 FormGroup 或 FormArray 添加到现有的 FormGroup 中 - Reactive Forms: How to add new FormGroup or FormArray into an existing FormGroup at a later point in time in Angular 7/8/9 Angular 2 formControlName在formGroup的子组件中 - Angular 2 formControlName inside a child component of formGroup 如何从 FormControlName 中获取 FormGroup? - how to get FormGroup from FormControlName? 如何获取值在 Angular 中更改的字段的 FormControlName 反应 forms - How to get FormControlName of the field which value changed in Angular reactive forms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM