Created
December 2, 2016 14:06
-
-
Save ste2425/ab179194cb9db633cc040df947e80f53 to your computer and use it in GitHub Desktop.
Prevent validation on disabled and hidden elements
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
.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