首页 > 解决方案 > 从父组件访问子组件中的表单组

问题描述

我正在尝试从父组件中的组件访问放置在组件内的多个 formGroup

@Component({
selector:'parent',
template:'<child1></child1>
<child2></child2>'
})

@Component({
selector:'child1',
template:'<div class="errors"></div>' //Need access to form1 and form2 in 
 this component
})

@Component({
selector:'child2',
template:'<form [formGroup]="form1"</form>
 <form [formGroup]="form2"</form>'
})

我需要访问“child1”中“child2”组件中的表单以获取验证错误

提前致谢

标签: angularangular5

解决方案


我会在父级中创建表单。然后您可以在子组件中注入 FormGroupDirective。

constructor(private fd: FormGroupDirective){}

形式是this.fd.form


推荐阅读