Skip to content

Instantly share code, notes, and snippets.

@ste2425
Created December 2, 2016 14:06
Show Gist options
  • Save ste2425/ab179194cb9db633cc040df947e80f53 to your computer and use it in GitHub Desktop.
Save ste2425/ab179194cb9db633cc040df947e80f53 to your computer and use it in GitHub Desktop.
Prevent validation on disabled and hidden elements
.directive('ignoreDisabled', ['$exceptionHandler', ($exceptionHandler) => {
return {
restrict: 'A',
require: 'ngModel',
link: ($scope, elem, attr, ctrl) => {
if (!attr.hasOwnProperty('ngDisabled')) {
$exceptionHandler(new ReferenceError('Attempt to use ignoreDisabled on element without ngDisabled.'));
} else {
$scope.$watch(attr.ngDisabled, (val, oldVal) => {
if (val === true && val !== oldVal) {
ctrl.$setUntouched();
ctrl.$setPristine();
Object.keys(ctrl.$validators)
.forEach((key) => ctrl.$setValidity(key, true));
} else {
Object.keys(ctrl.$validators)
.forEach((key) => ctrl.$setValidity(key, false));
}
});
}
}
};
}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment