Angular 5 Scroll to top on every Route click

后端 未结 18 1880
遇见更好的自我
遇见更好的自我 2020-11-29 17:49

I am using angular 5. I have a dashboard where I have few sections with small content and few sections with so large content that I am facing a problem when changing router

18条回答
  •  旧巷少年郎
    2020-11-29 18:25

    I keep looking for a built in solution to this problem like there is in AngularJS. But until then this solution works for me, It's simple, and preserves back button functionality.

    app.component.html

    
    

    app.component.ts

    onDeactivate() {
      document.body.scrollTop = 0;
      // Alternatively, you can scroll to top by using this other call:
      // window.scrollTo(0, 0)
    }
    

    Answer from zurfyx original post

提交回复
热议问题