3

If you have a tag like:

   <a href="/hello" ng-click="func()"> Hello </a>

Which would take priority, ng-click or href?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ninja
  • 1,012
  • 3
  • 14
  • 29

2 Answers2

3

There is no case of priority here between href and click event (unless you are talking about the priority of default action of anchor and click event). ng-click is a directive which just underneath registers event handler to the DOM element and when you click on it, click event is getting triggered. The default action (browser's) of an anchor is to navigate to the url (if present, absolute or relative) specified in the href. And click of an anchor triggers default action to occur unless it is prevented by event.preventDefault() or return false(won't work with handlers registered with angular) (which does not work with angular), which is probably what you are look for.

In angular you can pass the event from ng-click as ng-click="func($event)" and you can use that in your handler or you can even use it inline.

From W3c

Some events are specified as cancelable. For these events, the DOM implementation generally has a default action associated with the event. An example of this is a hyperlink in a web browser. When the user clicks on the hyperlink the default action is generally to active that hyperlink. Before processing these events, the implementation must check for event listeners registered to receive the event and dispatch the event to those listeners. These listeners then have the option of canceling the implementation's default action or allowing the default action to proceed. In the case of the hyperlink in the browser, canceling the action would have the result of not activating the hyperlink.

Cancelation is accomplished by calling the Event's preventDefault method. If one or more EventListeners call preventDefault during any phase of event flow the default action will be canceled.

Community
  • 1
  • 1
PSL
  • 123,204
  • 21
  • 253
  • 243
1

Actually i just tried it my self and just post my result, because the other answer doesn't really clear it up:

The href will execute before the ng-click has the chance to be called. For example:

$scope.redirect = function(){
     window.location.href = 'http://www.stackoverflow.com';
};
<a href="http://www.google.com" ng-click="redirect()">Redirect</a>

This will lead to Google instead of stackoverflow.

malifa
  • 8,025
  • 2
  • 42
  • 57
  • oops then event.preventDefault() should never have worked for anybody. anchor navigation is a default behavior on click of an anchor. Now what happens? http://plnkr.co/edit/GoVKbz?p=preview If navigation has already happened before ng-click handler has been invoked the example as well should right? – PSL Oct 09 '14 at 21:25
  • I didn't downvote your answer and i didn't say you were wrong. Just you didn't quite answer the question in my opinion. It's like shooting a goal in soccer while getting fouled. The goal doesn't count because you'll get a penalty. But you still shot the goal first, it just got prevented afterwards. – malifa Oct 09 '14 at 21:34
  • When did i say you downvoted? Also i dont think your answer is correct in any means as well. I would be happy to improve my knowledge with an official doc that supports it though.. it got prevented afterwards? what got prevented? after redirection? Just have a look at the network console yourself. Even if you say it got prevented later who takes the priority, its the handler and the event because the specific default action prevention is checked before doing default action of the anchor, doesn't the property name itself kick something? – PSL Oct 09 '14 at 21:36
  • Hey penalty- sudden death, i have linked official source in my answer.. I am sure that will clear up things for you. So all the handlers attached to the event executes before proceeding with the default action... There is another simple test you can do instead of href just place a console.log and see if it gets logged or not. if your statement is correct `The href will execute before the ng-click has the chance to be called.` you wont see it logged because it would have already got redirected to another page. Did you upvote yourself? :D – PSL Oct 09 '14 at 21:51
  • aww damn, thats why...because i get what you're saying, but...this little info. ;) and no i didn't upvote myself. can you actually do that? ;) mh i maybe should delete my answer then. – malifa Oct 09 '14 at 22:06
  • No i was kidding :D. But your comments actually let me dig deeper to see why you see the behavior in your post.. So thanks for that.. :) cheers!! So its a draw!! no more extra time... :D – PSL Oct 09 '14 at 22:08