Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2: How to provide interpolation with [routerLink]

I have routing defined in routing.ts file in this way.

const routesapp: Routes= [
{path:'user/id', component:UserComponent}
];
export const:routing ModuleWithProviders = RouterModule.forRoot(routesapp, {useHash:true});

and in HTML

<li class ="Some class">
    <a href= "#/user/{{id}}"> Link </a>
</li>

How do I convert this to work with [routerLink]? From previous posts I learnt that we cannot add interpolation with [routerLink], i.e [routerLink] = ['user/{{id}}']

I want to add interpolation in HTML only and I cannot add it in routing file. Also, How to override useHash of routing file in HTML?

like image 599
Protagonist Avatar asked Feb 08 '17 03:02

Protagonist


People also ask

What is difference between routerLink and routerLink?

What is the difference between [routerLink] and routerLink ? How should you use each one? They're the same directive. You use the first one to pass a dynamic value, and the second one to pass a static path as a string.

Can we use href instead of routerLink?

In this case since they are served at two different base urls(in this case for dev environment its localhost:4201 && localhost:4202) using router link will not actually route you to the other base route location. Using href is required or it will fail routing you to the correct url.

What does routerLink do in Angular?

In Angular, RouterLink is a directive for navigating to a different route declaratively. Router. navigate and Router. navigateByURL are two methods available to the Router class to navigate imperatively in your component classes.


1 Answers

try this

<li class ="Some class">
    <a [routerLink]="['user', idVariable]">Link </a>
</li>
like image 77
Tiep Phan Avatar answered Nov 16 '22 01:11

Tiep Phan