javascript - 如何根据 Observable 属性呈现弹出消息?

标签 javascript angular typescript observable typescript1.8

我想在传递可观察值时显示一条弹出消息,它是一个字符串可观察值。

这是我的函数,它返回可观察的字符串:

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/

相关文章:

javascript - 单例无法跨多个文件 Nodejs 工作

javascript - 如何确保可以从 IE 的新窗口中读取 cookie?

angular - ionic 3 : hide ion-footer on login page

angular - 如何在 Angular 2 中使用 react 形式启用按钮?

javascript - 在页面 ionic 2 中使用 app.component 函数

javascript - 用表示大于Number.MAX_SAFE_INTEGER的值的字符串调用parseInt

javascript - 无法使用 Angular 6 删除空标签

javascript - 有没有办法从输入类型中删除箭头但将其范围仅限于特定组件?

javascript - Angular2中嵌套多个组件之间的通信

javascript - 无法检索全日历事件的时间和日期