0
사용자 지정 양식 유효성 검사를 위해 지시문을 만들고 입력이 유효한지 여부를 확인했습니다. 경우에 따라 여러 오류가 발생할 수 있으며 HTML에 너무 많은 ng-messsage 문을 쓰고 싶지 않습니다.AngularJs : 자바 스크립트에서 오류 메시지를 반환하는 방법?
내가 원하는 곳은 html로 한 곳이고 오류는 javascript에서 반환됩니다.
function strongSecret() {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attr, ctrl) {
// please note you can name your function & argument anything you like
function customValidator(ngModelValue) {
// check if contains uppercase
// if it does contain uppercase, set our custom `uppercaseValidator` to valid/true
// otherwise set it to non-valid/false
if (/[A-Z]/.test(ngModelValue)) {
ctrl.$setValidity('uppercaseValidator', true);
} else {
ctrl.$setValidity('uppercaseValidator', false);
}
// check if contains number
// if it does contain number, set our custom `numberValidator` to valid/true
// otherwise set it to non-valid/false
if (/[0-9]/.test(ngModelValue)) {
ctrl.$setValidity('numberValidator', true);
} else {
ctrl.$setValidity('numberValidator', false);
}
// check if the length of our input is exactly 6 characters
// if it is 6, set our custom `sixCharactersValidator` to valid/true
// othwise set it to non-valid/false
if (ngModelValue.length === 6) {
ctrl.$setValidity('sixCharactersValidator', true);
} else {
ctrl.$setValidity('sixCharactersValidator', false);
}
// we need to return our ngModelValue, to be displayed to the user(value of the input)
return ngModelValue;
}
// we need to add our customValidator function to an array of other(build-in or custom) functions
// I have not notice any performance issues, but it would be worth investigating how much
// effect does this have on the performance of the app
ctrl.$parsers.push(customValidator);
}
}
}
나는 각도가 특정 오류에 대한 메시지를 설정하는 방법이나 이와 유사한 메시지를 표시합니다.
콘솔 로그를 찾고 있지 않습니다. html로 동적 텍스트 메시지와 함께 하나의 메시지 - 메시지 진술을 원합니다. angularjs에 오류 메시지가 설정되어야합니다. 예