Working with the latest version of Angular.
I have a directive when I am setting a custom validator and also monitoring the ngModel for changes. $scope.$watch is only working when the validator returns true and not when it returns false. I'm curious why this is and what the alternative is.
The goal is that the validator should set the form to be valid when the right conditions are met. The $scope.watch() is intended to do additional text formatting as the user is typing, i.e. only allowing number inputs for example.
app.directive('validatorDirective', [
function ($timeout) {
return {
restrict: 'A',
require: 'ngModel',
link: function ($scope, $element, $attr, $controller) {
$scope.$watch($attr.ngModel, function (val) {
// no output when validator returns false, but returns model updates when validator returns true
console.log(val);
});
$controller.$validators.validFn = function (modelValue, viewValue) {
return false;
};
}
}
}
}]);