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.

5
  • See this answer. Commented Apr 20, 2018 at 21:44
  • See this answer. Looping isn't really needed. Commented Apr 20, 2018 at 21:45
  • 2
    Possible duplicate of Angular 4.3 - HttpClient set params Commented Apr 21, 2018 at 1:04
  • Put your params in an arguments of constructor 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. Commented Apr 21, 2018 at 3:45
  • 1
    @KelvinLai: The solution worked, thanks a lot. Commented Apr 24, 2018 at 21:34

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.