2

I'm on page 1, I scroll halfway through the page and then click a link that brings me to page 2 through the Angular router. When I hit the "back" button, I'd like page 1 to be scrolled halfway down, just like it was before I left that page.

What is the easiest way to do that?

alcfeoh
  • 2,147
  • 1
  • 18
  • 33
  • Maybe you could create service in which you will write last scroll position and 'pageId' on component ngDestroy, and next time on ngInit check if there is a scroll positions. – Igor Janković Feb 24 '17 at 13:28
  • Sounds like a reasonable solution. I was wondering if Angular had an "out of the box" way to do this through some router config, since it is a default browser behavior that we lose while using the router. – alcfeoh Feb 24 '17 at 13:54

1 Answers1

0

Save to localStorage scroll position in ngOnDestroy(), and restore it in ngOnInit()

Ivan
  • 876
  • 1
  • 8
  • 22
  • Can anyone explain why this is downvoted? Is it because the answer is so concise that it is almost unhelpful, or is it because it's a bad way to solve the problem? I'm trying to solve a similar problem, and I really wish the downvoters had left a comment... – pbristow Nov 15 '18 at 13:20
  • i did something similar. Just instead of saving it in `localStorage` i wrote a own service. – marcel Dec 05 '18 at 13:07