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

AngularAngular2 RoutingAngular2 Router3

Angular Problem Overview


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?

Angular Solutions


Solution 1 - Angular

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

Solution 2 - Angular

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']

Solution 3 - Angular

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}`);
  });



Solution 4 - Angular

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()

Solution 5 - Angular

It's very simple

<a routerLink="/user/{{id}}/details"></a>

Solution 6 - Angular

constructor(private activatedRoute: ActivatedRoute) {

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

This works for me!

Solution 7 - Angular

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

Solution 8 - Angular

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 –

Solution 9 - Angular

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThorsten WestheiderView Question on Stackoverflow
Solution 1 - AngularWojciech KwiatekView Answer on Stackoverflow
Solution 2 - AngularRıdvanView Answer on Stackoverflow
Solution 3 - AngularRejwanul RejaView Answer on Stackoverflow
Solution 4 - AngularmakinyelureView Answer on Stackoverflow
Solution 5 - AngularAvtandil KavrelishviliView Answer on Stackoverflow
Solution 6 - AngularPedro EuropeuView Answer on Stackoverflow
Solution 7 - AngularMohammad Zaid PathanView Answer on Stackoverflow
Solution 8 - AngularDyaryView Answer on Stackoverflow
Solution 9 - AngularJorgeView Answer on Stackoverflow