且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

更改路线不会滚动到新页面的顶部

更新时间:2023-10-28 22:41:10

问题在于您的 ngView 在加载新视图时保留了滚动位置.您可以指示 $anchorScroll 在视图更新后滚动视口"(docs 有点含糊,但在此处滚动意味着滚动到新视图的顶部).

解决方案是将 autoscroll="true" 添加到您的 ngView 元素:

I've found some undesired, at least for me, behaviour when the route changes. In the step 11 of the tutorial http://angular.github.io/angular-phonecat/step-11/app/#/phones you can see the list of phones. If you scroll to the bottom and click on one of the latest, you can see that the scroll isn't at top, instead is kind of in the middle.

I've found this in one of my apps too and I was wondering how can I get this to scroll to the top. I can do it mannually, but I think that there should be other elegant way to do this which I don't know.

So, is there an elegant way to scroll to the top when the route changes?

The problem is that your ngView retains the scroll position when it loads a new view. You can instruct $anchorScroll to "scroll the viewport after the view is updated" (the docs are a bit vague, but scrolling here means scrolling to the top of the new view).

The solution is to add autoscroll="true" to your ngView element:

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