angular scroll to top on route change code example
Example 1: scroll to top angular
<router-outlet (activate)="onActivate($event)"></router-outlet>
export class AppComponent {
onActivate(event) {
window.scroll(0,0);
}
}
Example 2: angular route change scroll to top
RouterModule.forRoot(appRoutes, { scrollPositionRestoration: 'enabled' })
Example 3: scroll to top when routing angular
import { Component, OnInit } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';
@Component({
selector: 'my-app',
template: '<ng-content></ng-content>',
})
export class MyAppComponent implements OnInit {
constructor(private router: Router) { }
ngOnInit() {
this.router.events.subscribe((evt) => {
if (!(evt instanceof NavigationEnd)) {
return;
}
window.scrollTo(0, 0)
});
}
}