RxJS Observables - 发出任何新值时调用函数

时间:2017-04-08 06:53:00

标签: javascript angular typescript ionic2 rxjs

我遵循了从firebase DB中获取JSON数据的observable:

this.http.get('https://testdb.firebaseio.com/.json').map(res => res.json()).subscribe(result => {
  // do something with result
});

我希望在第一次从数据库中拉出 值后,在observable中调用一个函数foo()。要清楚,我只有在值到达时才会foo()"稍后"。

例如:

this.http.get('https://testdb.firebaseio.com/.json').map(res => res.json()).subscribe(result => {

  // Only called when a new value arrives in the stream later, not on the first pull when the stream is first activated.
  foo();

});

稍后,我的意思是每当订阅数据库更新时。我也不需要更新的实际值,我只想在任何新值到来时触发一个函数(不带参数)。

1 个答案:

答案 0 :(得分:-1)

我没有使用firebase的经验,但如果你想skip第一个值,那么yourObservable.skip(1)就可以了。 yourObservable可能是您的数据来源,即:

this.http.get('https://testdb.firebaseio.com/.json')
.map(res => res.json())
.skip(1)
.subscribe((result) => foo();)