Ответ 1
Ниже приводится пошаговое объяснение. Обратите внимание, что документация действительно хороша: страницы форм и $parsers - это те, искать.
link: function(scope, elm, attrs, ctrl) {
/**
* This function is added to the list of the $parsers.
* It will be executed the DOM (the view value) change.
* Array.unshift() put it in the beginning of the list, so
* it will be executed before all the other
*/
ctrl.$parsers.unshift(function(viewValue) {
scope.pwdValidLength = (viewValue && viewValue.length >= 8 ? 'valid' : undefined); // Check the length of the string
scope.pwdHasLetter = (viewValue && /[A-z]/.test(viewValue)) ? 'valid' : undefined; // Check if the string contains letter. RegExp.test() simply returns a boolean if the string matches the regex.
scope.pwdHasNumber = (viewValue && /\d/.test(viewValue)) ? 'valid' : undefined; // Check if the string contains digit. Same remark.
if(scope.pwdValidLength && scope.pwdHasLetter && scope.pwdHasNumber) { // If all is good, then…
ctrl.$setValidity('pwd', true); // Tell the controlller that the value is valid
return viewValue; // Return this value (it will be put into the model)
} else { // … otherwise…
ctrl.$setValidity('pwd', false); // Tell the controlller that the value is invalid
return undefined; // When the value is invalid, we should return `undefined`, as asked by the documentation
}
});
}