I have a filter with checkboxes where after checking added parameter to the url like this
https://localhost:8080/api/records?page=0&locationIds=1,2
But want to be like this
https://localhost:8080/api/records?page=0&locationIds=1&locationIds=2
As I understood thats 'cause I pass an Array to URL parameter and my main thought is to rewrite this forEach not for 'key', but for 'httpParams[key]' in file candidate.service.ts(not sure) but don't know how to make it right.
Here is my code:
filter.service.ts. (In this service filter returns as array)
filterByValues(filterType: string, value): FilterParams {
if (!this.filterParams[filterType]) {
this.filterParams[filterType] = [value];
} else if (this.isValueInParams(this.filterParams[filterType], value)) {
this.filterParams[filterType] = this.removeValueFromParams(this.filterParams[filterType], value);
} else {
this.filterParams[filterType].push(value); //return [1, 2]
}
return this.filterParams;
}
isValueInParams(params, value) {
return params.some(item => value === item);
}
I use this service in other file, when I check some of checkboxes, this id added to Array
candidate-filter.ts
filterByLocation(location: Location) {
const filtersToEmit = this.filterService.filterByValues(CandidateFilter.LOCATION, location.id);
this.candidatesHttpParams.emit(filtersToEmit);
}
Here where I add the url:
candidate.service.ts
get(page: number, httpParams: FilterParams): Observable<Candidate[]> {
let params = new HttpParams().set('page', page.toString());
//My main thought is to rewrite this forEach not for 'key', but for 'httpParams[key]'. Not sure
Object.keys(httpParams).forEach(key =>
params = params.append(key, httpParams[key])); //key = locationIds, httpParams[key] = [1,2]
return this.http.get<Candidate[]>(this.url, { params });
}
And where actually get Candidates
import-page.ts
private getCandidates(page: number) {
this.candidateViewService.get(page, this.params)
//this.params = {key: Array(2)} // [1, 2]
.subscribe(
(candidates: CandidateView[]) => this.candidates = candidates,
error => this.logger.error(error.message));
console.log('this Params',this.params)
}
Hope my question is clear. Would be really grateful for any help!

https://localhost:8080/api/records?page=0&locationIds=1&locationIds=2? It's for a get call you can create your url string as you want by appending to your URL all your params.idpushed tothis.filterParamsarray which goes to filecandidate.service.ts. In this serviceForEachworks forkeyand return url like this.../records?page=0&locationIds=1,2&otherKey=2,5and then inimport-page.tsI call thisgetmethod to get my url. But need to get url like this.../records?page=0&locationIds=1&locationIds=2&otherKey=2..