Home > Enterprise >  Specifying multiple values for a parameter in Angular
Specifying multiple values for a parameter in Angular

Time:07-07

Does Angular support sending multiple values for a single query parameter. In theory it should as it provides convertToParamMap method that can get all values for a single parameter.

I was trying to use the following syntax in my html template:

<a [routerLink]="['/first-component']" [queryParams]="{param1:'value1, value2'}" routerLinkActive="active" >First Component</a>

... however the value 'value1, value2' always comes as one value - not two as I would want it to.

My question is : What is the correct syntax for that?

CodePudding user response:

Should probably be an array of strings like: [queryParams]="{param1:['value1', 'value2']}"

CodePudding user response:

i believe the params of queryparams is stored as an objects, so you should just properly filter through the object to retrieve..

or the syntax for your call "{param1:'value1, value2'}" is invalid.

What have you tried? anything like changing to "{param1: 'value1', param2: 'value2'}" ?

some helpful resources:

https://www.digitalocean.com/community/tutorials/angular-query-parameters

https://www.geeksforgeeks.org/how-to-pass-query-parameters-with-a-routerlink/

CodePudding user response:

[queryParams]="{param1:['value1', 'value2']}"

You can do it like this but you will get an array like this

constructor(
    private ar: ActivatedRoute
  ) { }

  ngOnInit(): void {
    this.ar.queryParams.subscribe((params) => {
      console.log(params) // ['value1', 'value2']
    })
  }
  • Related