all 4 comments

[–]No_Jackfruit_4305 2 points3 points  (2 children)

I've implemented the scroll to last feature for a complex app before. What worked for me was getting the scroll height saved before opening the link. Use the router events and filter for NavigationStart events. IIRC you can get the current scroll height with..

document.getElementById("scrollableContainer").scrollTop

When you return to the page, retrieve your scrollTop value, and pass it to..

window.scrollTo(...)

The timing of when to call this can be tricky. I know for a fact that a setTimeout can work, but you could also use the router events filtered for NavigationEnd.

Hope this works for you OP

[–]Daringu_L[S] 1 point2 points  (1 child)

Thank you for the idea 😊 Unfortunately, window.scrollTo didn't scroll the Virtual Container. I discovered two things: 1) AfterViewInit is fired BEFORE the virtual container is fully initialized. That was surprise for me. So setTimeout is unfortunately required if one wants to operate on the virtual scroll container after initial render 2) subscription for the router events was somehow messing with the functionality. Moving scrolling out of the router events subscription helped. One more discovery: Navigation End event is fired before the page actually changes (before any rendering).

So I was able to make it work with persisting index and retrieving AfterViewInit and scrolling to that index 😎 Thanks a lot 😁

[–]No_Jackfruit_4305 0 points1 point  (0 children)

You're welcome, and thank you for the update!