2017-04-20 99 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); 
    });