简体   繁体   English

角度路线激活时如何制作适当的锚标签

[英]How to make proper anchor tags when angular route is active

I'm trying to set up some anchor tags for a template, which gets modified by angular route and has the following url scheme: 我正在尝试为模板设置一些锚标记,该模板会通过angular route进行修改,并具有以下url方案:

main.html#/view/:name

I tried to ignore angular's routing by setting the target attribute: 我试图通过设置target属性来忽略angular的路由:

 <a target="_self" href="#some_anchor">Go to anchor</a>

however, the actual link now is main.html#/some_anchor , and omitting target=_self doesn't change anything. 但是,实际的链接现在是main.html#/some_anchor ,而省略target=_self不会改变任何内容。
I then tried to change the links target to the actual current url with 然后,我尝试使用以下方法将链接目标更改为实际的当前网址

$location.path() + "/#some_anchor"

which doesn't work either (no matter if I set target attribute or not), I still get a "Page not found" error. 哪一个都不起作用(无论是否设置target属性),我仍然会收到“找不到页面”错误。

I have to make this workaround in the same situation: 我必须在相同的情况下进行此变通方法:

Define function in scope: 在范围内定义功能:

$scope.scrollTo = function (id) {
    var old = $location.hash();
    $location.hash(id);
    $anchorScroll();
    $location.hash(old);
};

And call it this way: 并这样称呼:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM