272

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?

Thorsten Westheider
  • 10,572
  • 14
  • 55
  • 97
  • 2
    The array of URL fragments consists of all parts of the route you want to redirect to. We have to keep the same fragments order as in the route definition. More details about this and how to redirect using Router or RouterLink directive here: https://stackoverflow.com/questions/37880876/how-to-pass-query-parameters-with-a-routerlink#comment130056069_37880876 – Maciej Wojcik Sep 08 '22 at 12:52

9 Answers9

451

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

DJDaveMark
  • 2,669
  • 23
  • 35
Wojciech Kwiatek
  • 6,634
  • 2
  • 16
  • 26
65

Maybe it is really late answer but if you want to navigate another page with param you can,

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

also you shouldn't forget about routing config like ,

 [path: 'user/:id/details', component:userComponent, pathMatch: 'full']
Rıdvan
  • 720
  • 7
  • 11
23

First configure in table:

const routes: Routes = [  
{
  path: 'class/:id/enrollment/:guid',
  component: ClassEnrollmentComponent
 }
];

now in type script code:

this.router.navigate([`class/${classId}/enrollment/${4545455}`]);

receive params in another component

 this.activatedRoute.params.subscribe(params => {
  let id = params['id'];
  let guid = params['guid'];

  console.log(`${id},${guid}`);
  });
Rejwanul Reja
  • 1,339
  • 1
  • 17
  • 19
10

It's very simple

<a routerLink="/user/{{id}}/details"></a>
Avtandil Kavrelishvili
  • 1,651
  • 3
  • 27
  • 37
7

There are multiple ways of achieving this.

  • Through [routerLink] directive
  • The navigate(Array) method of the Router class
  • The navigateByUrl(string) method which takes a string and returns a promise

The routerLink attribute requires you to import the routingModule into the feature module in case you lazy loaded the feature module or just import the app-routing-module if it is not automatically added to the AppModule imports array.

  • RouterLink
<a [routerLink]="['/user', user.id]">John Doe</a>
<a routerLink="urlString">John Doe</a> // urlString is computed in your component
  • Navigate
// Inject Router into your component
// Inject ActivatedRoute into your component. This will allow the route to be done related to the current url
this._router.navigate(['user',user.id], {relativeTo: this._activatedRoute})
  • NavigateByUrl
this._router.navigateByUrl(urlString).then((bool) => {}).catch()
makinyelure
  • 165
  • 2
  • 7
2
constructor(private activatedRoute: ActivatedRoute) {

this.activatedRoute.queryParams.subscribe(params => {
  console.log(params['type'])
  });  }

This works for me!

  • in the case you would use this.activatedRoute.parmas.subscribe(params => { console.log(params['type']) }); } so params instead of query paramas to get the parameter from the url – Dyary Apr 05 '21 at 21:03
  • 1
    This doesn't answer the question. You're not using routerLink to create a link here. You're just parsing queryParams if there are any ... – AlxVallejo Feb 12 '23 at 18:32
1

app-routing.module.ts

const routes: Routes = [
  { path: 'products', component: ProductsComponent },
  { path: 'product/:id', component: ProductDetailsComponent },
  { path: '', redirectTo: '/products', pathMatch: 'full' },
];

In controller you can navigate like this,

this.router.navigate(['/products', productId]);

It will land you to path like this: http://localhost:4200/products/product-id

Ref

Mohammad Zaid Pathan
  • 16,304
  • 7
  • 99
  • 130
0
constructor(private activatedRoute: ActivatedRoute) {}

in the case you would use

`this.activatedRoute.parmas.subscribe(params => { console.log(params['type']) }); }`

so params instead of queryparamas to get the parameter from the url –

Amit Soni
  • 3,216
  • 6
  • 31
  • 50
Dyary
  • 753
  • 7
  • 14
0

You can use dynamic values to generate the link. For a dynamic link, pass an array of path segments, followed by the params for each segment. For example, ['/team', teamId, 'user', userName, {details: true}] generates a link to /team/11/user/bob;details=true.

Jorge
  • 77
  • 4