Angular 7.x.x easiest way to scroll to a fragment? Angular 7.x.x easiest way to scroll to a fragment? typescript typescript

Angular 7.x.x easiest way to scroll to a fragment?


I had this same issue. Check out this solution by Ben Nadel. The solution is for Angular 7.X. You can configure RouterModule to handle anchor scrolling, even works with multiple clicks on the anchor with fragment. Here's the code you need:

 @NgModule({  imports: [RouterModule.forRoot(routes,            {              anchorScrolling: 'enabled',              onSameUrlNavigation: 'reload',              scrollPositionRestoration: 'enabled'            })],  exports: [RouterModule]})export class AppRoutingModule { }

Hope this helps.


⚠️ anchorScrolling: 'enabled is not working when you use *ngIf or something similiar see GitHub issue 1 and GitHub issue 2. ⚠️

A modern one line sultion for this problem: https://stackoverflow.com/a/64185407/5356110