美文网首页
AngularJS 如何完成在本页的id跳转

AngularJS 如何完成在本页的id跳转

作者: 南国青天 | 来源:发表于2016-12-08 10:42 被阅读97次

html:在需要跳转的地方添加scrollTo()参数是跳转的id.


<a href="" ng-click="scrollTo('foo')">Scroll to #foo</a>

<div id="foo">
  This is #foo
</div>

JavaScript: 在你的控制器完成你的跳转操作.(这需要声明$location, $anchorScroll)

app.controller('MainCtrl', function($scope, $location, $anchorScroll, $routeParams) {
  $scope.scrollTo = function(id) {
     $location.hash(id);
     $anchorScroll();
  }
});

相关文章

网友评论

      本文标题:AngularJS 如何完成在本页的id跳转

      本文链接:https://www.haomeiwen.com/subject/dqzjmttx.html