Angular JS ng-view change store scroll position

user2263197 picture user2263197 · May 7, 2014 · Viewed 8k times · Source

I used button click to change the route to the next page. Angular JS retain the scroll position and doesn't scroll to the top of the ng-view. 1. Disabling $anchorScroll doesn't work 2. Using window scroll to doesn't work

$rootScope.on("$routeChangeSuccess", function(){
     window.scrollTo(0,90);
})

However, to keep the fix header on iPad, the following window scroll works.

$(document).on('blur', 'input, textarea', function() {
                    setTimeout(function() {
                        window.scrollTo(document.body.scrollLeft,document.body.scrollTop);
                    }, 0);
                });

Any suggestions? How to clear the stored scroll positions by angualar js view?

Answer

Rafael picture Rafael · Aug 12, 2014

What you want is not disable anchorscroll, but enable it. You can achieve what you want using the following code:

<div data-ng-view data-autoscroll="true"></div>

or:

<div data-ng-view data-autoscroll></div>

as pointed in the AngularJS docs.