Search code examples
angulartypescriptsubscribequery-stringangular-activatedroute

Angular get query param and call service


I've got a problem in my angular app. I have to call a service reading some params from url. It's not working because the service is fired before the subscription to have the params is finished. In my service file I have this:

constructor(private http: HttpClient, private route: ActivatedRoute) { 
    this.route.queryParams.subscribe(params => {
      this.param1 = params['param1'];
      this.param2 = params['param2'];
    });
  }

and then the service:

getConfigs() {
    let configPath = baseUrl + this.param1 + "/" + this.param2;
    return this.http.get<any>(configPath);
  }

so, in my AppComponent I call the getConfigs() service but it's not working because the two params are undefined. How can I fix it? That's how I call the service in AppComponent

this.service.getConfigs().subscribe((configData) => {
      this.configParams = configData;
    });

Solution

  • Take the query parameters from router, and use the first() operator to get only the first event and then use switchMap() to get the data with params option.

      constructor(
        private _http: HttpClient,
        private _route: ActivatedRoute,
      ) { }
    
      getConfigs() {
        return this._route.queryParams.pipe(
          // rxjs operator skip empty object
          filter(params => !!Object.keys(params).length),
          // rxjs operator use only first event
          first(),
          // rxjs operator switch to another observable
          switchMap(params => this._http.get('host', { params })),
        );
      }