2017-04-20 8 views
0

私はcomponentを動的に作成し、@OuputEventEmitterの1つに加入しています。角2:ダイナミックコンポーネントのイベントエミッタサブスクリプション、購読を解除する必要がありますか?

関連するコード:

動的コンポーネント:

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

はい、動作しています。私は周りを少し掘り下げた後、これが動的コンポーネントの@Ouput()を処理する方法であることを発見しました。 – Thibs

+0

彼らが一度実行してから彼らが風に乗っているので、イベントや約束を拒否する必要はありません。あなたはObservablesを使うときに何を心配する必要があります。 – unitario

答えて

0

私はあなたが退会する必要があるかどうかはわかりませんが、私がtake(1)を実行して観察可能なことを完了した場合に備えてください。

confirmDialogRef.instance.buttonClicked 
    .take(1) 
    .subscribe(val => { 
     this.appRef.detachView(confirmDialogRef.hostView); 
     resolve(val); 
    }); 
関連する問題