I have written a method in Angular, and I executed it by passing the argument q=’status’. However, the first console.log(params) did not include q=’status’, whereas the second console.log(params) included it. Why is that?
getHospitalProfile(urlName: string, q: string | null = null): Observable<any> {
let params = new HttpParams();
if (q !== null) {
params = params.set('q', q);
console.log(params);
}
params = params.set('q', 'status');
console.log(params);
return this.http.get(`${this.apiUrl}/hospitals/${urlName}`, {
...this.httpOptions,
observe: 'response',
params: params
})
.pipe(
map((res: HttpResponse<any>) => {
if (res.status === 200) {
return res.body;
}
}),
catchError(error => {
throw error('ng');
})
);
}
2
Answers
I'm sorry. There was no problem with the implementation in question and the issue was caused by something else. Thank you to everyone who provided answers.
Use
.append(q)
..set()
is for replacement.