2

我有一个接受并显示自定义 NgTemplate 的 Angular 组件(ParentComponent)。我希望 ParentComponent 通过 EventEmitter 从 NgTemplate 中的组件接收事件。像这样的东西:

父组件.html

<!-- I want onCustomEvent to be called -->
<ng-container *ngTemplateOutlet="childTemplate" (customEvent)='onCustomEvent($event)'>
</ng-container>

其他组件.html

<ng-template #child>
  <button (click)='customEvent.emit("Hello World")'>Emit</button>
</ng-template>
<parent-component [childTemplate]='child'></parent-component>

这是我能想到的最简单的一个例子,所以希望它足以理解这一点。这样的事情可能吗?如果不是,可以使用哪些策略来完成同样的事情?

4

0 回答 0