I am using Angular 5 and have the following problem:
I have a route which contains a parameter year. The related component should now display the data for that year which is retrieved by a service. When first routing to such an URL, this works fine. If I change the parameter year, the component is not reloaded (as expected), just OnInit() is called again. In this method, I try to reload the data by calling the service method with the new year, but it does not work. Here is my code:
test.component.ts:
public ngOnInit() {
this.route.paramMap.subscribe( params => {
if (params.has('year')) {
this.loadData(params.get('year'));
} else {
this.loadData();
}
});
}
public loadData(year?: string): void {
this.testService.getData(year).subscribe(data => {
// edit and set to variables
});
}
test.service.ts:
public getData(year) {
return Observable.forkJoin(
this.getPart1(year),
this.getPart2(year)
);
}
private getPart1(year): Observable<CustomObject[]> {
let url = this.baseUrl;
if (year) url = url.concat("?year=" + year);
return this.http.get<CustomObject[]>(url, httpOptions).pipe(
catchError(this.handleError<CustomObject[]>())
)
}
private getPart2(year): Observable<OtherCustomObject[]> {
let url = this.baseUrl + '/path';
if (year) url = url.concat("?year=" + year);
return this.http.get<OtherCustomObject[]>(url, httpOptions).pipe(
catchError(this.handleError<OtherCustomObject[]>())
)
}
How to force the Observable to reload the HTTP request? I tried using a (Replay)Subject instead of Observables, but that didn't work.
Any help is highly appreciated :)