I am working with Angular 6 project in which I have disabled/removed hash-location-strategy which removes # from URL.
due to this change the link having:
<li routerLinkActive="active">
<a [routerLink]="['/settings']">Contact Settings</a>
<ul class="child-link-sub">
<li>
<a href="#contactTypes">Contact Types</a>
</li>
</ul>
</li>
is no more working it just skips current components URL and puts #contactTypes after localhost.
I found this link which should solve the issue using
<a [routerLink]="['/settings/']" fragment="contactTypes" >Contact Types</a>
which puts #contactTypes at the end of the URL but it doesn't scroll to the top of the browser.
You need to use hash routing strategy to enable the hash scrolling.
you need to give second argument as an object like RouterModule.forRoot([],{useHash:true}}. It will work.
use ngx page scroll
I was looking for a similar solution and tried to use the ngx-scroll-to package and found that its not working in latest version of angular so decided to look into other option and found that we can use
scrollIntoView
HTML code :
Ts code :
Angular 6.1 comes with an option called
achorScrolling
that lives in router module'sExtraOptions
. As theanchorScrolling
definition says:You can use it like that: