My component currently calls a service in the following way:
this.resultService.getResults().subscribe(
results => {
this.results = results;
this.searchResults = results.SearchResults;
this.searchResults = this.processResults(this.searchResults);
},
error => this.errorMessage = <any>error
);
The service makes use of HttpClient to fetch data from a website. As of now, I have a fixed URL defined in the service for testing purposes. Now that I know the service is functioning correctly and returning results, I am looking to dynamically pass the query string to be incorporated into the URL instead of hardcoding it. Is there a way to achieve this? Below is the snippet of code from my service:
private resultsUrl = 'https://testingsite.com/api/orgs/search?primaryCategory=plumber&city=denver&stateProvince=CO&PageNumber=1';
constructor(private http: HttpClient) { }
getResults(): Observable<any> {
return this.http.get<any>(this.resultsUrl, httpOptions).pipe(
tap(data => console.log('Response Data: ' + JSON.stringify(data))),
catchError(this.handleError)
);
}
private handleError(err: HttpErrorResponse) {
let errorMessage = '';
if (err.error instanceof ErrorEvent) {
errorMessage = 'An error occurred: ${err.status}, error message is: ${err.message}';
}
console.error(errorMessage);
return throwError(errorMessage);
}