我想在传递可观察值时显示一条弹出消息,它是一个字符串可观察值。
这是我的函数,它返回可观察的字符串:
public sendUpdate() {
this._mtService.sendCurrentUpdate(this.myList).subscribe(
res => this.messageToDisplay = res,
error => this.messageToDisplay = error
);
}
这是我的功能,我会在弹出窗口中显示相关消息:
public showMessageDialog(message) {
let config = new MdDialogConfig()
.title('Message:')
.textContent(message)
.ok('Got it');
this.dialog.open(MdDialogBasic, this.element, config);
}
现在,我想知道当可观察对象准备就绪时,我应该在哪里以及如何调用此消息来呈现 messageToDisplay
。
如果你能告诉我如何在可观察对象等待接收字符串时显示一些加载程序,然后当它出现时显示它,那就更好了...
我尝试这样做:
public sendUpdate() {
this._mtService.sendCurrentUpdate(this.myList).subscribe(
res => this.messageToDisplay = res,
error => this.messageToDisplay = error
);
this.showMessageDialog(this.messageToDisplay);
}
但是这里发生的情况是,第一次单击更新时,我看到一个空的弹出窗口,如果我再次单击它,我会看到带有消息的弹出窗口,很明显,这是因为字符串没有出现而发生的还没回来,但是我该如何克服它?
谢谢!
最佳答案
您传递给 subscribe()
的函数将稍后/异步调用,因此您还需要稍后调用 showMessageDialog()
:
public sendUpdate() {
this.showLoader();
this._mtService.sendCurrentUpdate(this.myList).subscribe(
res => { this.stopLoader(); this.showMessageDialog(res); },
error => { this.stopLoader(); this.showMessageDialog(error); }
);
}
关于javascript - 如何根据 Observable 属性呈现弹出消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38832032/