RxJs get value from observable
To get data from an observable, you need to subscribe: this.singleEvents$.subscribe(event => this.event = event); In the template you can directly bind to observables using the async pipe: {{singleEvents$ | async}}
To get data from an observable, you need to subscribe: this.singleEvents$.subscribe(event => this.event = event); In the template you can directly bind to observables using the async pipe: {{singleEvents$ | async}}
The parallel operator proved to be a problem for almost all use cases and does not do what most expect from it, so it was removed in the 1.0.0.rc.4 release: https://github.com/ReactiveX/RxJava/pull/1716 A good example of how to do this type of behavior and get parallel execution can be seen here. In your example code it … Read more
Using the following imports: import {Observable} from ‘rxjs/Observable’; import ‘rxjs/add/observable/of’; import ‘rxjs/add/operator/delay’; Try this: let fakeResponse = [1,2,3]; let delayedObservable = Observable.of(fakeResponse).delay(5000); delayedObservable.subscribe(data => console.log(data)); UPDATE: RXJS 6 The above solution doesn’t really work anymore in newer versions of RXJS (and of angular for example). So the scenario is that I have an array of … Read more
JohnWowUs’ answer is great and helped me understand how to avoid the recursion effectively, but there were some points I was still confused about, so I’m posting my tweaked version. Summary: The individual pages are returned as a Single. Use a Flowable to stream each of the items contained in the pages. This means callers … Read more
tl;dr; mergeMap is way more powerful than map. Understanding mergeMap is the necessary condition to access full power of Rx. similarities both mergeMap and map acts on a single stream (vs. zip, combineLatest) both mergeMap and map can transform elements of a stream (vs. filter, delay) differences map cannot change size of the source stream … Read more
Taking this from a previous answer: flatMap/mergeMap – creates an Observable immediately for any source item, all previous Observables are kept alive. Note flatMap is an alias for mergeMap and flatMap will be removed in RxJS 8. concatMap – waits for the previous Observable to complete before creating the next one switchMap – for any … Read more
UPDATE: 9/24/16 Angular 2.0 Stable This question gets a lot of traffic still, so, I wanted to update it. With the insanity of changes from Alpha, Beta, and 7 RC candidates, I stopped updating my SO answers until they went stable. This is the perfect case for using Subjects and ReplaySubjects I personally prefer to … Read more
update (2.0.0 final) (somepath/:someparam/someotherpath) you can subscribe to them using _router.queryParams.subscribe(…): export class HeroComponent implements OnInit { constructor(private _activatedRoute: ActivatedRoute, private _router:Router) { _activatedRoute.queryParams.subscribe( params => console.log(‘queryParams’, params[‘st’])); original If you want queryParams instead of route params (somepath/:someparam/someotherpath) you can subscribe to them using _router.routerState.queryParams.subscribe(…): export class HeroComponent implements OnInit { constructor(private _activatedRoute: ActivatedRoute, private … Read more
This can be achieved using the switchMap operator. This example uses RxJS 5.5+ pipeable operators. import { switchMap } from ‘rxjs/operators’; registerUser(user: User) { return this.utility.getIpAddress().pipe( switchMap(data => { this.ipAddress = data.ip; const body = { UserName: user.UserName, Email: user.Email, UserIP: this.ipAddress, }; return this.http.post(this.registerAPI, body); }) ) } RxJS < 5.5: import { switchMap … Read more
For transforming items emitted by an Observable into another Observable, you probably want to use the mergeMap operator. It creates an inner Observable and flattens its result to the outer stream. const source = this.myService .getFoo() .pipe( mergeMap(result => this.myService.getMoo(result)) ) .subscribe(result2 => { // do some stuff }); Here are some flat operators you … Read more