Lambda
Lambda

Reputation: 1640

Angular router navigate set query parameter name from variable

I would like to set the query parameter name dynamically.

this.router.navigate([alternativeRedirect],{ queryParams: { supportContractId: p },})

Is it possible to set supportContractId dynamically:

let parameterName = 'supportContractId'

this.router.navigate([alternativeRedirect],{ queryParams: { parameterName: p },})

Upvotes: 0

Views: 3418

Answers (2)

thisdotutkarsh
thisdotutkarsh

Reputation: 980

According to the ECMAScript2015 language specification, you can create objects with computed keys.

Therefore you can directly do the following,

let parameterName = 'supportContractID'
this.router.navigate([alternativeRedirect],{ queryParams: { [parameterName]: value })

Upvotes: 1

TotallyNewb
TotallyNewb

Reputation: 4790

You can define the params object beforehand and set the properties dynamically using the property accessor (aka bracket notation). It would be something along those lines:

let params = {};
params[parameterName] = p;
this.router.navigate([alternativeRedirect],{ queryParams: params})

Upvotes: 2

Related Questions