Autoscroll in Angular 2

后端 未结 10 1169
清歌不尽
清歌不尽 2020-12-01 12:21

I\'m experiencing an issue with Angular 2 where changing from one route to another does not automatically scroll to the top of the new view. I realize that Angular 1 allowed

10条回答
  •  离开以前
    2020-12-01 13:08

    I'm using material sidenav and I couldn't get any of the suggested answers to work for me. Here's my working solution:

    import { Router, NavigationEnd } from '@angular/router';
    
    ...
    
    constructor(
      private router: Router,
    ) {
      router.events.subscribe(event => {
        if (event instanceof NavigationEnd) {
          document.querySelector('.mat-sidenav-content').scrollTop = 0;
        }
      }
    }
    

提交回复
热议问题