Angular 2 and RxJS

angular rxjs

622 просмотра

2 ответа

I am trying to build an example realtime app with Angular 2.0.0RC5 and RxJS 5.0.0-beta.6. Although I it is all working with this code:

import {IntervalObservable} from 'rxjs/observable/IntervalObservable';
return IntervalObservable.create(1000)
                .flatMap(() => this.http.get('filename.json'))

Is this the correct way? The current RxJS api docs seem to specify different methods. Is there a way to upgrade to the latest version of RxJS without breaking everything?

What happened to the Observable.interval() function? I have seen several examples using this.

Anything you can offer to shine a bit of light onto this would be great.

Many thanks


Автор: JT-Helsinki Источник Размещён: 08.11.2019 11:11

Ответы (2)

2 плюса


Observable.interval is just a shortcut to IntervalObservable.create:

Автор: Harry Ninh Размещён: 20.08.2016 10:49

0 плюса

Thanks Guys, so this worked.

import { Observable }   from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/observable/concat';
import {IntervalObservable} from 'rxjs/observable/IntervalObservable';


private serverGetRequestContinuous(requestInterval: number, jsonFileName: String): Observable<any> {
        return Observable.concat(Observable.of(null), IntervalObservable.create(10000))
            .flatMap(() => this.http.get('filename.json'))

By concatenatinge these two Observables there is no delay for the first request. If IntervalObservable.create(requestInterval) was called alone, the polling would start only after the specified requestInterval. This first Observable causes a single request right now and thus no delay in displaying data in the UI. The second Observable polls every 10 seconds.

Автор: JT-Helsinki Размещён: 20.08.2016 01:06
Вопросы из категории :