Angular 嵌套在ng内容中的目标组件
我试图将嵌套在Angular 嵌套在ng内容中的目标组件,angular,Angular,我试图将嵌套在ng content中的组件作为目标,并像ViewChild那样处理它。例如,ComponentB放在ComponentA的ng content中 我试图做的类似于: @ViewChild('componentB')componentBViewChild:componentB; 因此,我可以使用它的功能,如: this.componentBViewChild.functionFoo(); 问题是,我无法添加引用以将嵌套组件作为目标 欢迎提供任何帮助使用@ContentChild
ng content
中的组件作为目标,并像ViewChild
那样处理它。例如,ComponentB
放在ComponentA
的ng content
中
我试图做的类似于:
@ViewChild('componentB')componentBViewChild:componentB;
因此,我可以使用它的功能,如:
this.componentBViewChild.functionFoo();
问题是,我无法添加引用以将嵌套组件作为目标
欢迎提供任何帮助使用
@ContentChild
而不是@ViewChild
来引用投影内容@ViewChild
仅查找直接在组件的HTML模板中找到的元素
例如,给定具有以下HTML的组件a
:
其用途如下:
您可以从ComponentA
中引用ComponentB
,如下所示:
@ContentChild(ComponentB)ComponentB:ComponentB;
可以找到文档