您正在寻找
$anchorScroll()。
这是(糟糕的)文档。
这是来源。
基本上,您只需将其注入并在控制器中调用它,它就会将您滚动到ID为的任何元素
$location.hash()
app.controller('TestCtrl', function($scope, $location, $anchorScroll) { $scope.scrollTo = function(id) { $location.hash(id); $anchorScroll(); }});<a ng-click="scrollTo('foo')">Foo</a><div id="foo">Here you are</div>这是一个演示的朋克
编辑:与路由一起使用
像往常一样设置角度布线,然后只需添加以下代码。
app.run(function($rootScope, $location, $anchorScroll, $routeParams) { //when the route is changed scroll to the proper element. $rootScope.$on('$routeChangeSuccess', function(newRoute, oldRoute) { $location.hash($routeParams.scrollTo); $anchorScroll(); });});并且您的链接如下所示:
<a href="#/test?scrollTo=foo">Test/Foo</a>
这是一个Plunker,展示了通过路由和$
anchorScroll进行滚动
甚至更简单:
app.run(function($rootScope, $location, $anchorScroll) { //when the route is changed scroll to the proper element. $rootScope.$on('$routeChangeSuccess', function(newRoute, oldRoute) { if($location.hash()) $anchorScroll(); });});并且您的链接如下所示:
<a href="#/test#foo">Test/Foo</a>



