Created
February 19, 2016 10:47
-
-
Save ste2425/0f3ed164120bc2da51e6 to your computer and use it in GitHub Desktop.
Click handler that will not fire if element is disabled. (anchor tags)
This file contains hidden or 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
/* | |
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