Angularjs 中的自定义验证器 - $validators 未显示在 link 函数中
Custom Validators in Angularjs - $validators is not showing in link function
我正在尝试创建自定义验证器,但我发现 ctrl.$validators 未定义。下面是我的代码:
link: function (scope, element, attrs, ctrl) {
ctrl.$validators.emptyCheckboxList = function (modelValue, viewValue) {
if (!modelValue || modelValue.length === 0) {
ctrl.$setValidity("emptyCheckboxList", false);
}
else {
ctrl.$setValidity("emptyCheckboxList", true);
}
}
}
此外,有没有办法覆盖所需的验证器?在 AngularJS 的文档中,他们提到要覆盖 $isEmpty,但它对我不起作用。
谢谢
$validators
道具在 ngModelController
中可用(ng-model
指令的控制器)。似乎您在指令定义对象中缺少 require ngModel
。
require: 'ngModel', //<-- this must be present here
link: function (scope, element, attrs, ctrl) {
ctrl.$validators.emptyCheckboxList = function (modelValue, viewValue) {
if (!modelValue || modelValue.length === 0) {
ctrl.$setValidity("emptyCheckboxList", false);
}
else {
ctrl.$setValidity("emptyCheckboxList", true);
}
}
}
当您在指令 DDO 中添加 require: 'ngModel'
时。它在 link
函数的第 4 个参数内使用 require
指令控制器(本例 ngModelController
)。
我正在尝试创建自定义验证器,但我发现 ctrl.$validators 未定义。下面是我的代码:
link: function (scope, element, attrs, ctrl) {
ctrl.$validators.emptyCheckboxList = function (modelValue, viewValue) {
if (!modelValue || modelValue.length === 0) {
ctrl.$setValidity("emptyCheckboxList", false);
}
else {
ctrl.$setValidity("emptyCheckboxList", true);
}
}
}
此外,有没有办法覆盖所需的验证器?在 AngularJS 的文档中,他们提到要覆盖 $isEmpty,但它对我不起作用。
谢谢
$validators
道具在 ngModelController
中可用(ng-model
指令的控制器)。似乎您在指令定义对象中缺少 require ngModel
。
require: 'ngModel', //<-- this must be present here
link: function (scope, element, attrs, ctrl) {
ctrl.$validators.emptyCheckboxList = function (modelValue, viewValue) {
if (!modelValue || modelValue.length === 0) {
ctrl.$setValidity("emptyCheckboxList", false);
}
else {
ctrl.$setValidity("emptyCheckboxList", true);
}
}
}
当您在指令 DDO 中添加 require: 'ngModel'
时。它在 link
函数的第 4 个参数内使用 require
指令控制器(本例 ngModelController
)。