Angular 向动态加载的组件添加事件处理程序

Angular 向动态加载的组件添加事件处理程序,angular,typescript,dynamically-generated,eventemitter,Angular,Typescript,Dynamically Generated,Eventemitter,我有两个组件:工作流和块。使用指令和组件工厂将块组件动态加载到工作流 块组件包含一个按钮,我想在单击按钮时向父级(工作流)发送事件,因此我在块组件中添加了@Output('onDelete')onDelete=new EventEmitter(),以便能够发送事件 我遇到的问题是在从angular开始的通用示例中添加事件处理程序 这个问题的具体例子 private createNewBlockComponent(事件,对象):void{ const componentFactory=this.c

我有两个组件:
工作流
。使用
指令
组件工厂
组件动态加载到
工作流

组件包含一个按钮,我想在单击按钮时向父级(
工作流
)发送事件,因此我在
块组件
中添加了
@Output('onDelete')onDelete=new EventEmitter()
,以便能够发送事件

我遇到的问题是在从angular开始的
通用示例中添加
事件处理程序

这个问题的具体例子
private createNewBlockComponent(事件,对象):void{
const componentFactory=this.componentFactoryResolver.resolveComponentFactory(BlockComponent);
const componentRef=this.workflowsDirective.viewContainerRef.createComponent(组件工厂);
(componentRef.instance).position=新块位置(event.layerX,event.layerY);
(componentRef.instance).onDelete.subscribe(()=>{
此.blockDeleted();
}) ;
}

您的代码片段似乎与您的问题不符。@jbrown为什么会这样
appWorkflowDirective
是我在
.ts
中使用的指令,用于动态加载
块组件
@Output
装饰程序允许您绑定到模板中的事件。但是您谈论的是动态组件,所以
@输出对您没有帮助。你能用ComponentFactory plz添加包含零件的代码吗?@NoémiSalaün我添加了代码。这就是我想要的,绑定到动态加载的组件发送的事件。
<div clas="mainContent">
  <ng-template appWorkflowDirective></ng-template>
</div>
private createNewBlockComponent(event, object): void {
   const componentFactory = this.componentFactoryResolver.resolveComponentFactory(BlockComponent);
   const componentRef = this.workflowsDirective.viewContainerRef.createComponent(componentFactory);

   (<BlockComponent>componentRef.instance).position = new BlockPosition(event.layerX, event.layerY) ;
}
const componentFactory = this.componentFactoryResolver.resolveComponentFactory(BlockComponent);

const componentRef = this.viewContainerRef.createComponent(componentFactory);

const blockInstance = componentRef.instance as BlockComponent;

blockInstance.onDelete.subscribe(() => {
    this.blockDeleted();
});
private createNewBlockComponent(event, object): void {
   const componentFactory = this.componentFactoryResolver.resolveComponentFactory(BlockComponent);
   const componentRef = this.workflowsDirective.viewContainerRef.createComponent(componentFactory);

   (<BlockComponent>componentRef.instance).position = new BlockPosition(event.layerX, event.layerY) ;

   (<BlockComponent>componentRef.instance).onDelete.subscribe(() => {
      this.blockDeleted();
   }) ;
}