2017-04-20 95 views
0

我正在动态创建component并订阅其中的一个@OuputEventEmitterAngular 2:动态组件的Event Emitter订阅,需要退订吗?

相关的代码:

动态组件:

export class DynamicComponent implements OnInit { 
    @Output() results: EventEmitter<any> = new EventEmitter<any>(); 
... 
} 

父组件:

private loadDynamicComponent(): void { 
    const componentFactory = this.factoryResolver.resolveComponentFactory(DynamicComponent); 
    const componentRef = this.host.viewContainerRef.createComponent(componentFactory); 

    (<DynamicComponent>componentRef.instance).results.subscribe(result => { 
     this.result = result; 
    }) 
} 

我感到困惑的我是否需要unsubscribe或将Angular打扫一下吗?

+0

我很惊讶你甚至得到了任何结果。我不认为你实际上可以订阅活动。你确定你的实施是正确的吗? – unitario

+0

是的,它工作。经过一番挖掘,我发现这是处理动态组件的@Output()方法。 – Thibs

+0

好的。没有必要退出事件或承诺,因为他们执行一次,然后他们随风飘逝。你只需要关心什么时使用Observables。 – unitario

回答

0

我不确定你是否需要取消订阅,但只是为了防止我(1)完成可观察项。

confirmDialogRef.instance.buttonClicked 
    .take(1) 
    .subscribe(val => { 
     this.appRef.detachView(confirmDialogRef.hostView); 
     resolve(val); 
    });