Sumit Chaudhari Sumit Chaudhari -4 years ago 143
Javascript Question

Format routerLink param url

In my angular 2 app I have defined a router link like :

<a *ngFor="let demo of demos" [routerLink]="['demo',]">example</a>

Currently I am getting as "". I want to format this as "" to show in the browser address bar.

Any help would be appreciated.

Answer Source

You can use functions inside routerLink. So you can use a function in your component:

    return str.replace(' ', '-');

And use it in your routerLink:

[routerLink]="['/demo', hyphenateUrlParams(]"

This provides much more re-usability than mutating variables directly inside the routerLink.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download