1

I am trying to send multiple parameters to HttpParams in Angular 5 using the following approach:

            paramsObject: any
            params = new HttpParams();
            for (let key in paramsObject) {
                params.set(key, paramsObject[key]);
            }

This worked in Angular 4, but in Angular 5 since HttpParams is immutable object the params are not being set to HttpParams and null parameters are being passed. Could you let me know how I can set multiple parameters to HttpParams. I am using Angular 5 and TypeScript.

Valla
  • 2,414
  • 11
  • 42
  • 73
  • See [this answer](https://stackoverflow.com/a/45426801/1009922). – ConnorsFan Apr 20 '18 at 21:44
  • See [this](https://stackoverflow.com/questions/48817781/overload-request-headers-and-params-with-httpclient-get/48818399#48818399) answer. Looping isn't really needed. – R. Richards Apr 20 '18 at 21:45
  • 2
    Possible duplicate of [Angular 4.3 - HttpClient set params](https://stackoverflow.com/questions/45210406/angular-4-3-httpclient-set-params) – Kelvin Lai Apr 21 '18 at 01:04
  • Put your params in an arguments of constructor https://angular.io/api/common/http/HttpParams#constructor or modify `params = params.set(key, para...)`, This class is immutable - all mutation operations return a new instance. – botika Apr 21 '18 at 03:45
  • 1
    @KelvinLai: The solution worked, thanks a lot. – Valla Apr 24 '18 at 21:34

1 Answers1

1

You need to reassign the params again:

paramsObject: any;
let params = new HttpParams();

for (let key in paramsObject) {
    params = params.set(key, paramsObject[key]);
}

return params;
Özer
  • 2,059
  • 18
  • 22