A> ngclick超过nghref上述&lt优先;分子 [英] prioritize ngclick over nghref in <a> elements

查看:246
本文介绍了A> ngclick超过nghref上述&lt优先;分子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL;博士:我怎么能强迫角度来只执行在&LT的ngclick指令; A>没有排空/删除元素的href

tl;dr: How can I force angular to only execute the ngclick directive in an <a> element without emptying/removing the href?

我的网站有一些元素一些自定义的行为(模式打开时,地址栏更新等),但对搜索引擎优化的索引我的需要它是一个&LT; A&GT ; HREF元素包含一个有效的链接属性。

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>

不过,棱角分明还执行的href 和路由,使我的自定义 NG-点击逻辑无用的。

However, angular also executes the href and the routing makes my custom ng-click logic useless.

有没有办法来反转这种行为?

Is there a way to invert this behavior?

推荐答案

您应该通过 $事件参数的onclick功能和执行即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();
}

例如:

<一个href=\"http://plnkr.co/edit/sKHST3GkRENtxptxr0mK?p=$p$pview\">http://plnkr.co/edit/sKHST3GkRENtxptxr0mK?p=$p$pview

这篇关于A&GT; ngclick超过nghref上述&lt优先;分子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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