In my form, I would like to set the form controls as untouched when the user focuses on them, to hide the validation messages that appear when the field is touched and invalid.
How can i do this?
I tried to write a directive, but could not get it to work. I see in the console that the value in the directive changes from true to false, but the form control is not updated.
HTML:
<form name="userForm" ng-submit="submitForm(userForm.$valid)" novalidate="">
<div class="form-group">
<label>Name*</label>
<input type="text" name="name" class="form-control" ng-model="user.name" untouch="userForm.name" />
<h3>Touched: {{userForm.name.$touched}}</h3>
</div>
</form>
Directive
validationApp.directive('untouch', function() {
return {
restrict : 'A',
require: 'ngModel',
scope: {
untouch : '='
},
link: function(scope, element) {
element.bind('focus', function() {
console.log(scope.untouch.$touched);
scope.untouch.$setUntouched();
console.log(scope.untouch.$touched);
});
}
};
});
Plunker
source
share