Last active
October 20, 2015 15:31
-
-
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
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
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