且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过包含插入(ng-content)插入的组件的Angular 2+调用函数

更新时间:2023-11-27 08:49:34

Chrispy的评论正确."...您可以将#templateRef添加到包含该元素的已嵌入组件(即不在ng-content上),然后可以将其用作选择器."

Chrispy's comment is correct. "...you can add the #templateRef to the transcluded component where it is being included (i.e. not on the ng-content), then you can use that as the selector instead."

所以我有我的父(模态)组件:

So I have in my parent (modal) component:

@ContentChild('transcludedContent') private transcludedContent: ModalContent;

然后将模态输入到哪里:

Then where the modal is brought in I have:

<modal>
  <transcluded-component #transcludedContent>
  </transcluded-component>
</modal>