angular2等到条件可观察到完成

kohli:

我已经实现了这样的if语句

if (this.service.check() ) {
      return true;
    }
    else {
}

如果条件等待后端的响应,则为this。但是在可观察对象执行之前,它会进入else语句并完成条件,而无需在启动时检查if条件。

这是我从后端获取数据的可观察方法

public check(){
      this.getActorRole().subscribe(
        (resp => {
          if (resp == true){
             return true
         }

        }),
        (error => {
          console.log(error);
        })
      );
    }
}

那么如何使条件等待直到它从后端获得响应

GünterZöchbauer:

您等不及ObservablePromise完成。您只能订阅它以在它完成或发出事件时得到通知。

public check(){
   return this.getActorRole() // <<<=== add return
   .map(resp => {
          if (resp == true){
             return true
         }
      );
    }
}

那你就可以

this.service.check().subscribe(value => {
  if (value) {
    return true;
  }
  else {
  }
}

但是如果此代码的调用者还取决于返回值,则再次需要

this.service.check()
.map(value => {
  if (value) {
    return true;
  }
  else {
  }
}

然后执行subscribe()哪里调用此代码

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章