How to pass a parameter to routerLink that is somewhere inside the URL?

Thorsten Westheider picture Thorsten Westheider · Jun 27, 2016 · Viewed 216.5k times · Source

I know I can pass a parameter to routerLink for routes such as

/user/:id

by writing

[routerLink]="['/user', user.id]"

but what about routes such as this one:

/user/:id/details

Is there a way to set this parameter or should I consider a different URL scheme?

Answer

Wojciech Kwiatek picture Wojciech Kwiatek · Jun 27, 2016

In your particular example you'd do the following routerLink:

[routerLink]="['user', user.id, 'details']"

To do so in a controller, you can inject Router and use:

router.navigate(['user', user.id, 'details']);

More info in the Angular docs Link Parameters Array section of Routing & Navigation