在 <a> 中,ngclick 优先于 nghref元素 [英] prioritize ngclick over nghref in <a> elements

查看:23
本文介绍了在 <a> 中,ngclick 优先于 nghref元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这篇关于在 &lt;a&gt; 中,ngclick 优先于 nghref元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆