0

I am beginner for angular 2 and i want to append hashtag at end of URL just like below :

http://localhost:4200/detail#one

I have tried this :

<a [routerLink]="['/detail#one']" >Link</a>

but # is being converted to '%23' :

http://localhost:4200/detail%23one

How can we do this in angular 2 to get URL like :

http://localhost:4200/detail#one

Thanks

Newbie
  • 787
  • 1
  • 7
  • 21

1 Answers1

1

You could use JavaScript's encodeURI function to keep the hashtag as a hashtag. This would require you to set up a variable in the class which that component belongs to.

const detailOneHash = encodeURI('#one');

In the routerLink, add the variable to the fragment attribute:

<a [routerLink]="['/detail']" attr.fragment="{{detailOneHash}}">Link</a>

You can find more information about routerLink in Angular's API.