How to pass an array within a query string in HttpClient?

前端 未结 11 894
面向向阳花
面向向阳花 2021-02-02 09:03

This is my array of actors:

[\'Elvis\', \'Jane\', \'Frances\']

How to pass this array within a query strin

11条回答
  •  无人共我
    2021-02-02 09:15

    I've used this method and it worked for me in Angular 10:

    private paramMaker(data) : HttpParams
    {
        let httpParams = new HttpParams();
        Object.keys(data).forEach((key) = > {
            if (Array.isArray(data[key]))
            {
                data[key].forEach((item, index) = > {
                    httpParams = httpParams.append(key + '[' + index + ']', item);
                });
            }
            else
            {
                httpParams = httpParams.append(key, data[key]);
            }
        });
        return httpParams;
    }
    

提交回复
热议问题