Skip to content

Instantly share code, notes, and snippets.

@ste2425
Created February 19, 2016 10:47
Show Gist options
  • Save ste2425/0f3ed164120bc2da51e6 to your computer and use it in GitHub Desktop.
Save ste2425/0f3ed164120bc2da51e6 to your computer and use it in GitHub Desktop.
Click handler that will not fire if element is disabled. (anchor tags)
/*
Will call your click handler if the element is not disabled.
Usefull for legacy apps where elements such as anchor tags are used.
As these do not respect the disabled attribute.
Does not matter how element is disabled, angular, jQuery native or even devTools.
Should strongly consider using it outside the `ng` namesapce.
*/
angular.module('').directive('ngAnchorClick', function ($parse) {
return {
restrict: 'A',
compile: function ($element, attr) {
var fn = $parse(attr['ngAnchorClick'], null, true);
// LinkFn
return function ($scope, elem) {
var nativeElem = elem[0];
nativeElem.addEventListener('click', function (e) {
if (nativeElem.hasAttribute('disabled')) {
e.stopPropagation();
return;
}
$scope.$apply(function () {
fn($scope, {
$event: e
});
});
});
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment