I know that this was given some time ago, but I had a similar problem, and I managed to assemble a suitable solution from many other answers and some trial and error. I suppose someone else might find something similar one day ...
Here is a method that (as far as I can tell) is directly related to the validation system. In this particular example, a match validation rule is created that compares two models and checks to see if their value matches.
<script type="text/javascript"> angular.module( "YourModule", [] ) .directive( "match", function() { return { require: 'ngModel', restrict: 'A', link: function( $scope, $elem, $attrs, $ctrl ) { // targetModel is the name of the model you want to // compare against. var targetModel = $attrs.match; // Add the 'match' validation method $ctrl.$validators.match = function( modelValue, viewValue ) { valid = $scope.$eval( targetModel ) == viewValue; $ctrl.$setValidity( 'match', valid ); return valid ? viewValue : undefined; }; // When the target model value changes, cause this model // to revalidate $scope.$watch( targetModel, function() { $ctrl.$validate(); } ); } }; } );
Then use this (obviously including form, ng-app and ng-controller ):
<input type="password" ng-model="password" /> <input type="password" ng-model="confirmation" match="password" />
The general idea is that when processing the correspondence directive, an additional function ( match ) is added to the $validators of the $ctrl object, where other validators (mandatory field, minimum length, ...) seem to live. This sets the check in the field, but this rule is only processed when updating the field with the compliance directive.
To combat this, the clock is then set on the target model. When the value of the target model is updated, it will run the $validate() method in the original control, allowing you to check the validity in either of the two fields.