在 <a> 中,ngclick 优先于 nghref元素 [英] prioritize ngclick over nghref in <a> elements
问题描述
tl;dr:如何强制 angular 只在 <a>
元素中执行 ngclick 指令而不清空/删除 href代码>?
tl;dr: How can I force angular to only execute the ngclick directive in an <a>
element without emptying/removing the href
?
我的网站在某些元素(模式打开、位置栏更新等)上有一些自定义行为,但对于 SEO 索引,我也需要它是 带有包含有效链接的
href
属性的 code> 元素.
My site has some custom behavior on some elements (modal opens, location bar updates, etc.) but for SEO indexing I also need it to be an <a>
element with an href
attribute containing a valid link.
例如:
<a href="/{{item.url}}" ng-click="doCustomStuff(item)">some link</a>
然而,angular 也会执行 href
并且路由使我的自定义 ng-click
逻辑无用.
However, angular also executes the href
and the routing makes my custom ng-click
logic useless.
有没有办法扭转这种行为?
Is there a way to invert this behavior?
推荐答案
您应该将 $event
参数传递给 onclick 函数并执行 e.preventDefault()
方法;
You should pass $event
parameter to onclick function and execute e.preventDefault()
method;
<a ng-click="clickHandler($event)" href="/home" >home</a>
在控制器中:
$scope.clickHandler = function(e){
e.preventDefault();
}
示例:
http://plnkr.co/edit/sKHST3GkRENtxptxr0mK?p=preview
这篇关于在 <a> 中,ngclick 优先于 nghref元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!