Created
August 16, 2012 18:33
-
-
Save boneskull/3372452 to your computer and use it in GitHub Desktop.
directive to prevent the default event action from occurring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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