Angular 访问子组件内的FormGroup

Angular 访问子组件内的FormGroup,angular,typescript,Angular,Typescript,我试图从另一个组件访问另一个组件中的表单组件。结构如下: ComponentA ------------------- ComponentB SubComponentA (This is where the form comes) 因此,从我的ComponentB中,我尝试访问SubComponentA,执行@ViewChild(ComponentA,{static:false})ComponentA:ComponentA,然后从ComponentB中的方法获取它,就像这样ComponentA

我试图从另一个组件访问另一个组件中的表单组件。结构如下:

ComponentA ------------------- ComponentB
SubComponentA (This is where the form comes)
因此,从我的ComponentB中,我尝试访问SubComponentA,执行
@ViewChild(ComponentA,{static:false})ComponentA:ComponentA
,然后从ComponentB中的方法获取它,就像这样
ComponentA.formGroup.reset(//data)
但它返回未定义的。如何在子组件TA中获取此formgroup?提前谢谢

StackBlitz:(我没有让它工作,第一次使用它,但它的结构,这就是我的意思


编辑:通过选择器调用表单,它会呈现表单,但我现在无法获取的是向表单传递数据。

您必须确保组件完全初始化了子组件视图

您应该从
ngAfterViewInit()中的子组件运行方法
不在
ngOnInit()

关于

在Angular 8中,当我们将
static
定义为true时,我们可以在
ngOnInit()
中使用子组件的属性或方法

例如:

@ViewChild( SubComponentAComponent, {static: true}) subcomponenta: SubComponentAComponent;

使用共享服务创建表单组。ComponentA和ComponentB是否共享同一个formGroup?如果是,可以将formGroup注入构造函数,也可以将其作为输入绑定传递。如果提供更多代码或stackblitz,这将很有帮助。谁是未定义的ComponentA或formGroup?put console.log(this.ComponentA)在重置调用之前,查看浏览器控制台中显示的内容。它将返回未定义的componentA调用。我正在尝试进行stackblitzStackBlitz Addddo您的意思是调用子方法ngAfterViewInit()从窗体还是从我试图调用它的组件中?在父组件中,您使用子组件时,我尝试了这一点,但没有成功,我之前错过的另一个细节是,通过选择器调用窗体可以很好地呈现窗体,但我无法获得向其传递数据的引用