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