【发布时间】:2015-06-03 10:07:36
【问题描述】:
我有一个角度指令用作验证输入的属性。
输入看起来像这样(玉)
input.form-control(type='text', name='subjectinput', id='subjectinput', subjectid-noprefix-validator='(subject.nosuffix==1)')
验证器看起来像
.directive('subjectidNoprefixValidator', function () {
return {
require: ['^form', 'ngModel'],
scope: {
noprefixformat: '=subjectidNoprefixValidator'
},
link: function (scope, element, attrs, ctrls) {
scope.form = ctrls[0]; // so we can use form reference in displaying error messages
var ngModel = ctrls[1];
ngModel.$validators.subjectidnoprefix = function (value) {
var status = true;
// removed some logic that sets status true or false
// using $scope.noprefixformat parameter
return status;
};
}
};
});
它工作正常。但是,有时在页面操作中会发生需要验证才能在 INPUT 上重新触发而不触及它的事件;一些单选按钮更改应该重新触发它并刷新验证状态(如您所见,我的验证器有一个参数)。
我尝试通过调用页面Controller来实现
$scope.subjCreationForm.subjectinput.$validate();
但它似乎不起作用。可能是什么情况?当它使用 $validators 时,是否有另一种方法可以以编程方式触发字段验证?
【问题讨论】:
-
这里stackoverflow.com/questions/30412661/…我对类似问题的回答。
标签: javascript angularjs validation