【发布时间】:2017-03-20 01:50:01
【问题描述】:
我用这个方法构建了一个简单的确认对话服务(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"));
如果有人使用该服务,他会返回一个 Subject(它是一个 Observable)并且可以“订阅()”它。单击“是”时会调用订阅,因此会给出确认...
这是正确的方法吗?更重要的是……会打电话给
this.confirmation.complete();
取消订阅已订阅的侦听器,从而防止任何延迟引用(内存泄漏)?
【问题讨论】:
-
我编辑了标题,因为
complete()方法不是 Obervable 接口的一部分。
标签: angular rxjs observable