Boland
Boland

Reputation: 1571

Angular JS retain scroll position after reload

I've got a ul > li list of items, with a reload button on top. If I hit the reload button, items are added async to the data model. Every time an item is added to the model, it is shown in the view, and the browser scrolls to the top. It's an Cordova app (HTML5). I can of course remember scroll position and scroll back, but that might become visible and is just a hack imo.

How can I make sure the scroll position stays at the same place?

Upvotes: 0

Views: 934

Answers (1)

Mikalai
Mikalai

Reputation: 1525

I think the problem is:

  • You click reload button
  • Your code clears array of items and it triggers angular to rebuild DOM, scroll automatically scroll to top

I suggest the solution, try to use $track by $index in angular ng-repeat https://docs.angularjs.org/api/ng/directive/ngRepeat

Upvotes: 1

Related Questions