首页 > 解决方案 > 3 秒后自动关闭对话框 angular 5 observables

问题描述

我有一个订阅,在该信号上接收一个信号,在我的 html 中显示一个对话框的布尔值设置为 true。我希望对话框在 3 秒后自动消失。

我在订阅中的 observable 遇到问题。

 this.updatedvolleyvar = this.venuevolly.receiveupdated()
      .subscribe(
        (req: any)=>{
          if (req != null){
            let stopcondition = false;
            this.showupdated = true;
            Observable.interval(3000)
              .takeWhile(() => !stopcondition)
                .subscribe(r =>{
                  this.showupdated = false;
                  stopcondition = true;
                });
          }
        }
      );

我相信我需要创建一个 observable 类型的对象,然后订阅它。我认为takeWhile这是让我失望的原因。接下来我该怎么办?

标签: angularrxjsangular2-observablessubscriptions

解决方案


您可以按照更简单,我认为更合适的方式关闭对话框 - 使用常规setTimeout

this.updatedvolleyvar = this.venuevolly.receiveupdated()
  .subscribe((req: any) => {
    if (req != null) {
      let stopcondition = false;
      this.showupdated = true;
      setTimeout(() => {
        this.showupdated = false;
        stopcondition = true;
      }, 3000);
    }
});

没有必要让你的生活复杂化,简单的事情就简单地完成了。


推荐阅读