【发布时间】:2018-06-04 16:06:55
【问题描述】:
有人知道是否可以针对除有界值之外的其他值进行自定义验证吗?
例如
我有一个 Type1 类型的对象数组,其中包含字段 Field1、Field2、Field3。
使用 ng-repeat 我渲染数组并绑定到 Field1 的一些输入。
如果可能使用 Angular 1.5,我需要实现的是验证 Field1 与 Field2 和 Field3 的关系。
更新:
这是我的自定义验证指令的外观:
app.directive('customValidationDirective', function () {
return {
require: 'ngModel',
link: function (scope, element, attr, mCtrl) {
debugger;
//here I can see/access data attributes e.g. attr.controlinfos
function validationHandler(value) {
debugger;
//here I have access only to new value in the input element
//this is what I need here
//I tried to access DOM element here but how can I pass it's name? because inputs are rendered dinamically with ng-repeat
if ('value meets condition in relation to some other values from attr.controlinfos')
{
mCtrl.$setValidity('invalid', true);
} else {
mCtrl.$setValidity('invalid', false);
}
return value;
}
mCtrl.$validators.push(validationHandler);
}
};
请看上面代码中的 cmets。
这就是我使用 HTML 的方式:
<form name="modelForm">
<table class="table span8">
<tbody>
<tr data-ng-repeat="control in value | filter: {'TagControlType': 'Input'}">
<td>
<span data-ng-bind="::control.TagName | prettyName" title="{{::control.TagDescription}}"></span>
<span style="color: red" data-ng-if="::control.TagMandatory">*</span>
<span> </span>
</td>
<td>
<input type="number" data-ng-if="control.InputType == 'Numeric'"
data-ng-model="control.TagValue" title="{{::control.TagDescription}}"
data-ng-class="{required: isInvalid(control)}"
data-ng-required custom-validation-directive
id="{{::control.TagName}}" />
</td>
</tr>
</tbody>
</table>
谢谢!
【问题讨论】:
-
您不能将 Field2 和 Field3 作为属性传递给您的自定义指令吗?
标签: angularjs angularjs-directive