我使用以下方法构建了一个简单的确认对话框服务(Angular 2):
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"));
如果有人使用该服务,他将返回一个主题(这是一个可观察对象),并且可以"订阅()"它.单击"是"时会调用订阅,因此会给出确认.
这是正确的方法吗?更重要的是...电话会打给我吗
this.confirmation.complete();
取消订阅订阅的侦听器,从而防止任何延迟引用(内存泄漏)?