angularjs 锚点操作服务 $anchorScroll

时间:2023-03-09 21:44:27
angularjs 锚点操作服务 $anchorScroll

在普通的html网页中,我们可以通过在url后边添加  #elementid 的方式,将页面显示定位到某个元素,也就是锚点。

但是在angularjs应用的网页中,页面路由的写法是 #route/route   锚点的写法会被当做一个页面路由解析过去,达不到定位的目的。

angular提供一个$anchorScroll  用来做锚点的功能。

用法如下:

$scope.goto = function (id) {
$location.hash(id);
$anchorScroll();
}

  

进入页面时或者触发某操作时调用 goto('id') 即可