Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
128
rated 0 times [  131] [ 3]  / answers: 1 / hits: 7593  / 10 Years ago, wed, march 19, 2014, 12:00:00

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?


More From » angularjs

 Answers
7

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


[#46721] Tuesday, March 18, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daytonm

Total Points: 519
Total Questions: 83
Total Answers: 89

Location: Saudi Arabia
Member since Mon, Sep 5, 2022
2 Years ago
;