html - Angular2 Hashtag路由到Anchor不会移动页面

标签 html angular anchor angular2-routing

所以我使用下面的帖子来弄清楚如何将片段添加到 url。

Angular2 Routing with Hashtag to page anchor

并且片段添加得很好,但是页面不会移动到生成的 anchor 。我尝试在目标 div 上同时使用 nameid 属性,但似乎都不起作用。

我使用的是 Angular 版本 2.0.0-rc.3 和路由器版本 3.0.0-alpha.6。

谢谢!

<a [routerLink]="[]" fragment="destination">Go TO DIV</a>

<div id='destination'>
    <h2>Destination</h2>
</div>

两个元素之间有足够的空间来判断页面是否实际移动。

如前所述,url 正确地将 #destination 添加到自身。

最佳答案

解决方法可能是在您的组件中编写一个函数,将 location.hash 设置为您要导航到的 div 的 ID。

<a (click)="goTo('destination')">Go TO DIV</a>

<div id='destination'>
    <h2>Destination</h2>
</div>

然后在你的组件中:

goTo(location: string): void {
    window.location.hash = location;
}

关于html - Angular2 Hashtag路由到Anchor不会移动页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38877353/

相关文章:

javascript - AngularJS:如何在路由之间以html形式保存数据

javascript - fatal error : Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory Angular 13 application

css - 在 IE8 中 anchor <a> 对齐关闭?

java - GWT 将一个元素插入另一个元素(跨入 anchor )

php - 如何使用php中的textarea向数据库中插入数据?

html - 有没有办法在 iOS 8 中加快 HTML 到 NSAttributedString 的渲染?

javascript - 使用 Javascript 将 CSV 从 URL 解析为数组

javascript - 链接 Angular 和 Node.js : TypeError: error. json 不是函数

angular - 输入数组的变化检测

javascript - 播放动画然后加载下一页