Angular Template: Use variable as object key

242 Views Asked by At

In a Typescript file you can do this:

const myProperty = 'id';
const queryParams = {[myProperty]: 5};

How can this be done in a Angular template? It should be something like this:

<div 
[routerLink]="../.." 
[queryParams]="{[myProperty]: 5}">
Navigate to page...
</div>

This doesn't work because of the square brackets around myProperty. Does anyone have an idea?

1

There are 1 best solutions below

0
GeoDev On

Maybe you want to implement link-parameters-arrays?

https://angular.io/guide/router#link-parameters-array

<a [routerLink]="['/crisis-center', { foo: 'foo' }]">Crisis Center</a>