【问题标题】:Angular 1.5 custom validation in relation to external valuesAngular 1.5 与外部值相关的自定义验证
【发布时间】: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>&nbsp;</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


【解决方案1】:

您希望字段 1 依赖于字段 2 和字段 3,对吗?

在不太了解您的变量等情况下,我创建了一个简单的解决方案。

模板:

<input class="field-one" ng-model='ctrl.fieldOne'></input>
<input class="field-two" 
       ng-change='ctrl.filterFieldOne()' 
       ng-model='ctrl.fieldTwo'></input>
<input class="field-three" 
       ng-change='ctrl.filterFieldOne()' 
       ng-model='ctrl.fieldThree'></input>

Ctrl:

class ctrl {
  /** @export {!Array<string>} **/
  this.fieldOne = [];

  /** @export {!Array<string>}  **/
  this.fieldTwo = [];

  /** @export {!Array<string>} **/
  this.fieldThree = [];

  /**
  * Takes in the controller variables (fieldTwo, fieldThree) and filters the fieldOne variable, and then resets fieldOne variable.
  */
  filterFieldOne() {
     const combinationFieldTwoFieldThree =  [...this.fieldTwo, ...this.fieldThree];
     const filterObj = this.fieldOne.filter(f => !combinationFieldTwoFieldThree.includes(f));
     this.fieldOne = Object.assign({}, filterObj);
  }

}

【讨论】:

  • 嗨,我更新了问题,这是自定义验证,我不需要过滤
猜你喜欢
  • 2016-09-28
  • 2021-06-26
  • 2019-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-28
  • 1970-01-01
  • 2013-02-18
相关资源
最近更新 更多