Home > OS >  URL building in angular
URL building in angular

Time:11-27

My backend accepts http://url/api/rooms?ids=1&ids=2&ids=3&ids=4 and I'm trying to do a URL builder. My attempt is below but it ignores the rest of the parameters.

deleteRoom(ids: number[]) {
  console.log(ids);

  let params = new HttpParams();
  for (var id of ids) {
    params = params.set('ids', id);
  }

  console.log(`${this.actionUrl}?${params.toString()}`);

  return this.httpClient.delete(`${this.actionUrl}?${params.toString()}`);
}

CodePudding user response:

In order to answer your question, it would be goog you to write down the result of your console.log(${this.actionUrl}?${params.toString()}); instruction.

Anyway, you could try to change .set for .append, like this:

params = params.append('ids', id);

NOTE: Just in case, I don't remember now if the first time you can do directly an "append", without had done a "set" before... So, only if the above solution did't work, try a "simple trick" like this:

let firstTime=true;

let params = new HttpParams();
  for (var id of ids) {
    params = firstTime ? params.set('ids', id) : params.append('ids', id);
    firstTime = false;
  }
  • Related