Angular 2: How to provide interpolation with [routerLink]

风流意气都作罢 提交于 2019-12-23 06:58:06

问题


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?


回答1:


try this

<li class ="Some class">
    <a [routerLink]="['user', idVariable]">Link </a>
</li>



回答2:


This router link worked for me [routerLink]="['/edit', element.ID]"

I used it in the following:

<a href="javascript: void(0);" [routerLink]="['/edit', element.ID]" 
routerLinkActive="router-link-active" > {{ element.RequesterName }}</a>

Thanks!



来源:https://stackoverflow.com/questions/42104040/angular-2-how-to-provide-interpolation-with-routerlink

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!