私はこの方法で簡単な確認ダイアログサービス(角2)を構築:Subject.complete()はすべてのリスナーを登録解除しますか?
confirm(body?: string, title?: string): Subject<void> {
this.confirmation = new Subject<void>();
// ... show dialog here... "are you sure?"
return this.confirmation;
}
_onYesClicked() {
// ... closing the dialog
this.confirmation.next();
this.confirmation.complete();
}
_onNoClicked() {
// ... closing the dialog
this.confirmation.complete();
}
使用法:
confirmationService.confirm().subscribe(() => alert("CONFIRMED"));
を誰かがサービスを使用している場合、彼は(観察可能である)件名を取得し、返さそれに "subscribe()"することができます。 「はい」をクリックすると、サブスクリプションが呼び出され、確認が行われました...
これは正しい方法ですか?そして、もっと重要な...
this.confirmation.complete();
への呼び出しが加入リスナーを解除し、したがって、任意の余韻の参照(メモリリーク)を防ぐことができますか?
'complete()'メソッドがObervableのインターフェースの一部ではないため、タイトルを編集しました。 –