prioritize ngclick over nghref in <a> elements

荒凉一梦 提交于 2019-12-10 14:53:03

问题


tl;dr: How can I force angular to only execute the ngclick directive in an <a> element without emptying/removing the 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.

Such as:

<a href="/{{item.url}}" ng-click="doCustomStuff(item)">some link</a>

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

Is there a way to invert this behavior?


回答1:


You should pass $event parameter to onclick function and execute e.preventDefault() method;

<a ng-click="clickHandler($event)" href="/home" >home</a>

and in controller:

$scope.clickHandler = function(e){
   e.preventDefault();
}

Example:

http://plnkr.co/edit/sKHST3GkRENtxptxr0mK?p=preview



来源:https://stackoverflow.com/questions/22504732/prioritize-ngclick-over-nghref-in-a-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!