【问题标题】:Angular2 observable http get conditional repeatAngular2 observable http 获取条件重复
【发布时间】:2017-12-09 12:55:56
【问题描述】:

我正在使用 angular2 observable 模式来发出 http 请求。 我正在尝试有条件地重复 http get:我想执行 http get 直到满足条件:

http.get('url')
.map(res => {
     // if the condition is met I should repeat the http get request
})
.subscribe()

有没有办法有条件地重复http get请求?

谢谢, 马可

【问题讨论】:

  • 我会说你可以为此使用递归函数
  • 嗨,迈克,你能解释一下我如何使用 observable 来做到这一点吗?

标签: angular http get rxjs observable


【解决方案1】:

您可以使用expand 运算符。这是一个例子:

let request$ = http.get('url');

request$.expand(value => {
  return value !== 0 ? request$ : Rx.Observable.empty()
})
.map(res => {
  //Do mapping here
})
.subscribe()

【讨论】:

    猜你喜欢
    • 2017-03-15
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    • 1970-01-01
    • 2017-01-18
    • 2017-09-14
    相关资源
    最近更新 更多