Is there a way to set an input validity inside a directive? The input present in the directive\'s template.
Lets say I have template:
You need to retrieve NgModelController instance associated with the input. Then you call $setValidity on this object specifying validation key (required, minlength, customkey, etc.). It will look like this:
.directive('myDir', function() {
return {
restrict: 'E',
template: '',
link: function(scope, element) {
var modelController = element.find('input').controller('ngModel');
someObj = {
val: '123'
};
scope.check = function() {
var result = false;
modelController.$setValidity('myrequired', result);
};
}
}
})
The most important part here is how to get NgModelController. Below line of code is taking care of it:
var modelController = element.find('input').controller('ngModel');