I've created a custom directive in AngularJS. The directive uses isolated scope, and it somehow prevents the binding for standard ngModel on the same element. I want to create a confirm password field (text for readability in the example).
<input type="text" name="one" ng-model="fields.field_one">
<input type="text" validate-match="fields.field_one" name="two" ng-model="field_two">
My directive invalidates the field, when there is no match.
app.directive('validateMatch', function() {
return {
require: 'ngModel',
scope: { matchValue: '=validateMatch' },
link: function(scope, elm, attr, ctrl) {
scope.$watch('matchValue', function(value) {
ctrl.$setValidity('match',
ctrl.$viewValue === value
|| !ctrl.$viewValue && !value);
});
function validate(value) {
ctrl.$setValidity('match', value === scope.matchValue);
return value;
}
ctrl.$parsers.push(validate);
ctrl.$formatters.push(validate);
}
}
});
The thing is, why can't I change the value of that field by changing the model? First field works just fine.
Look at the plunker for details and commented code.