A> ngclick超过nghref上述&lt优先;分子上述、分子、ngclick、GT

2023-09-13 04:49:45 作者:矜暮

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();
}

例如:

http://plnkr.co/edit/sKHST3GkRENtxptxr0mK?p=$p$pview