preventDefault() vs. stopPropagation() with click delegation

4,174 views
Skip to first unread message

Ben Nadel

unread,
Dec 20, 2012, 9:57:00 AM12/20/12
to ang...@googlegroups.com
I have a container. The container has a number of Anchor tags within it.

Based on some user interactions, that involve mouseDown / mouseUp events, I need to cancel the default behavior (ie. stop navigation) of the Anchor tags.

I'm using click-handler delegation (in a Directive) on the container:

container.on( "click", "a.marker", function( event ) {
. . . . event.preventDefault();.
} );


What I am finding is that this doesn't actually work. BUT, if I add event.stopPropagation() as well, it works.

Is AngularJS doing something special with click events on Anchor tags that requires this extra step?

Or, do I have a bug somewhere in my code that is forcing the navigation?

Thanks,
Ben Nadel

Peter Bacon Darwin

unread,
Dec 20, 2012, 4:52:28 PM12/20/12
to ang...@googlegroups.com
Angular overrides anchor tags with a directive: https://github.com/angular/angular.js/blob/master/src/ng/directive/a.js
But I don't this is responsible as it doesn't do anything unless href is missing.
Probably some jQuery magic.
Pete



--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
 
 

Ben Nadel

unread,
Dec 21, 2012, 8:49:05 AM12/21/12
to ang...@googlegroups.com
Yeah, its strange. I'm creating a directive to enable the drag/drop of "markers" on a page. The directive only handles mouse-interactions. But, it does a lot of mouseup, mousedown, mousemove, and click binding. So, it's definitely possible I'm just not seeing some bug in there.

I'll see if I can set up a Fiddle and reproduce at some point.

Thanks!
Ben Nadel
--
Ben Nadel
Adobe Community Expert
Adobe Certified Advanced ColdFusion Developer
Manager New York ColdFusion User Group
http://www.bennadel.com

Need ColdFusion Help?
http://www.bennadel.com/Ask-Ben
Reply all
Reply to author
Forward
0 new messages