How to keep scroll position after refreshing in AngularJS


When you add some items to the beginning of ng-repeated collection (probably with unshift method), AngularJS would refresh the view and take your adorable scrollbar to the top of it. Sometimes it can be naughty behaviour to you. So I'll show you how to fix this problem.

Example

Here is an example. If you want to try it, please take a look at jsFiddle.

It's a quite simple solution, that is, just  $watching scroll container's height. The increase of height means that your collection items were added and all DOM have been manipulated by AnuglarJS. The rest is easy, All you need to do is take the scroll bar to where it was ( newHeight - oldHeight).

I hope this could be of any help.

関連する記事


コメントを残す

メールアドレスが公開されることはありません。

CAPTCHA


このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください