In my Angular component ngOnInit()
I want to:
Both HTTP calls are always executing, but when my first observable is wired up to route.params
, the forkJoin(...).subscribe(...)
method never runs. If I replace this.route.params
with Observable.of({id: 1234})
forkJoin().subscribe()
gets called properly.
// VERSION 1 forkJoin().subscribe() never gets called
var dependentObservable = this.route.params
.switchMap(params => {
this.myId = +params['id'];
return this.myService.getMyInfo(this.myId);
});
// VERSION 2 forkJoin().subscribe gets called
var dependentObservable = Observable.of({id: 123})
.switchMap(params => {
this.myId = +params['id'];
return this.myService.getMyInfo(this.myId);
});
var independentObservable = this.myService.getOtherInfo();
Observable.forkJoin([dependentObservable, independentObservable])
.subscribe(
results = { ... },
error => { ... },
() => { ... }
);
Observables provide support for passing messages between parts of your application. They are used frequently in Angular and are the recommended technique for event handling, asynchronous programming, and handling multiple values.
Angular Route Params: How to Pass Route Params in Angular. To access route parameters and query parameters in Angular, use ActivatedRoute service. The ActivatedRoute service provides Observables through which we can subscribe to the values of route params and route query params. In Angular, we can pass the data as route parameters in two ways.
That is now an array of Observables. forkJoin is added as a wrapper in order for those calls to be made The end result can then be subscribed to which will output as a single array with the same count of items. NOTE: If one call fails in a forkJoin, THEY WILL ALL FAIL.
In Angular, we can pass the data as route parameters in two ways. Route params(Route parameters) Route queryParams(Route query parameters) If we want to pass values between views, then we can use route params. For example, if we’re going to pass an ID from one route to another and fetch the id on a component onInit(), then we can use route params.
My guess is route.params
is never completed. That's why forkJoin
does not work. You can use combineLatest
instead of forkJoin
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With