Skip to content

Instantly share code, notes, and snippets.

@richardTowers
Last active October 20, 2015 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richardTowers/316fccc3460a7a31fa25 to your computer and use it in GitHub Desktop.
Save richardTowers/316fccc3460a7a31fa25 to your computer and use it in GitHub Desktop.
A simple directive to add "matches" validation, e.g. to check if a password matches a confirmation
function crMatches () {
return {
require: 'ngModel',
scope: {
crMatches: '='
},
link: function(scope, elem, attrs, ctrl) {
scope.$watch('crMatches', function (newValue) {
if (newValue) { ctrl.$setValidity('matches', newValue === elem.val()); }
});
ctrl.$validators.matches = function (modelValue) { return scope.crMatches === modelValue; }
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment