javascript - 在 AngularJS 中滚动总是在顶部

标签 javascript jquery css angularjs mean.io

我正在使用 AngularJS(MEAN.io 堆栈),我遇到了一个与滚动相关的恼人故障。

当用户点击一个链接时,目标页面的滚动位置与原始页面相同,而不是停留在顶部。

为了修复这种奇怪的行为,我使用了 next:

$rootScope.$on("$viewContentLoaded", function () {
  $anchorScroll();
});

但是这段代码让每个页面都滚动到顶部位置,这也很烦人,因为用户必须能够返回并恢复之前的滚动位置。

有什么建议吗?我怎样才能得到想要的结果?

谢谢!

编辑--------------------

当我尝试使用 ng-view + autoscroll 解决方案时,它对我不起作用,我也不知道为什么。

这是我在服务器提供的 default.html 页面中使用它的方式:

<body ng-cloak class="ng-cloak" ng-class="{state: true, auth: authPage}" ng-controller="BodyController">

      <div ng-include="'/system/views/header.html'"></div>

    <section class="content">
      <div data-ng-include="'/ceh-admin/views/adminShortcuts.html'"></div>
      <div ng-view autoscroll="true">{% block content %}{% endblock %}</div>
    </section>

    <div data-ng-include="'/system/views/footer.html'"></div>

    {% include '../includes/foot.html' %}

  </body>

我的 HTML 代码中有什么奇怪的东西?

最佳答案

根据 ngView documentation您需要声明如何处理滚动。

autoscroll (optional) string Whether ngView should call $anchorScroll to scroll the viewport after the view is updated.

  • If the attribute is not set, disable scrolling.
  • If the attribute is set without value, enable scrolling.
  • Otherwise enable scrolling only if the autoscroll attribute value evaluated as an expression yields a truthy value.

默认情况下它是禁用的,因此您需要将以下内容添加到您的标记中:

<div ng-view autoscroll></div>

或(根据文档并根据您的喜好):

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

关于javascript - 在 AngularJS 中滚动总是在顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29752485/

相关文章:

javascript - 使用 jQuery 和 AJAX 填充表

javascript - 如何仅使用 javascript 更改按钮颜色 onclick?

javascript - 删除最后一些字符

javascript - 未捕获的类型错误 : Object has no method 'splice'

javascript - 浏览器选项卡之间的通信 : page titles must be updated synchronously for all browser tabs

javascript - jQuery:如何让回调函数只运行一次

javascript - 返回假;不工作

html - chrome 中的模糊文本

html - 图像的背景位置在 iPad 3 中无法正常工作

jquery - 恒星 js 不工作