Skip to content

Instantly share code, notes, and snippets.

@ggoodman
Created October 11, 2013 22:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ggoodman/6942981 to your computer and use it in GitHub Desktop.
Save ggoodman/6942981 to your computer and use it in GitHub Desktop.
Angular.js stop-propagation directive

Stop event propagation in Angular.js

This directive allows you to stop propagation for any event supplied.

Lets say you have an ng-click handler on a button that is overlayed on top of an <a href></a> link. You cant your click handler to fire, but not the anchor tag's default action. What you need is to stop the propagation of the click event up to the anchor tag.

Usage

<div ng-click="handleContainingElementClick()">
  <button ng-click="handleChildClick()" stop-propagation="click"></button>
</div>
module.directive "stopPropagation", ->
restrict: "A"
link: ($scope, $element, $attrs) ->
previousEventName = null
eventHandler = (e) -> e.stopPropagation()
$attrs.$observe "stopPropagation", (eventName) ->
$element.off previousEventName, eventHandler if previousEventName
$element.on eventName, eventHandler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment