Skip to content

Instantly share code, notes, and snippets.

@boneskull
Created August 16, 2012 18:33
Show Gist options
  • Save boneskull/3372452 to your computer and use it in GitHub Desktop.
Save boneskull/3372452 to your computer and use it in GitHub Desktop.
directive to prevent the default event action from occurring
/**
* preventDefault: prevents the default action of the event from occurring. This is useful if
* you are using anchor tags and hash routing, wherein you do not want the anchor click to screw
* with your hashes.
*
* Usage:
*
* <a href="#" prevent-default="click">click me</a>
*/
myModule.directive('preventDefault', function () {
return {
restrict: 'A',
link: function (scope, element, attribs) {
var handler = attribs.preventDefault;
if (!handler) {
throw new Error('preventDefault: please specify an event name to bind to');
}
// note that "on" requires jQuery; otherwise use bind or something
element.on(handler, function (evt) {
evt.preventDefault();
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment