Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On route change view doesn't scroll to top in the new page in angular2

I have tried autoscroll="false" on the router-outlet but does't seem to work, is there any default method of angular2 for doing the same without using any third party library?

like image 431
Pardeep Jain Avatar asked Dec 10 '22 14:12

Pardeep Jain


1 Answers

Until Angular 6

here is a more neat way to do it:

this.router.events.subscribe((ev:any) => {
  if (ev instanceof NavigationEnd) {
    window.scrollTo(0, 0);
  }
});

From Angular 6.1

You could do this in your router configuration. This is likely to be the norm in future versions as suggested here.

RouterModule.forRoot(routes, {scrollPositionRestoration: 'enabled'})
like image 65
Hamzeen Hameem Avatar answered Dec 13 '22 22:12

Hamzeen Hameem