0

我有一个ngRepeat填充表单,但 form.input.$error.pattern 为真(输入与RegExp不匹配)与第一个输入的文本输入中的任何内容。对于 form.input.$error.required 之后的所有输入,即使输入文本中存在某些内容,它也保持为真。.ng-pristine、.ng-touched、.ng-dirty 等类仍然按预期运行。

服务定义正则表达式:

.service('Apform', function () {
  'use strict';
var patt = {
            alpha: /^[a-zA-Z]+$/,
            alpha_numeric: /^[a-zA-Z0-9]+$/,
            phone: /^\d{3}[\-]\d{3}[\-]\d{4}$/,
            postal: /^[A-Za-z][0-9][A-Za-z] [0-9][A-Za-z][0-9]$/,
        };
return patt;

注入控制器:

控制器:功能($范围,Appform){

        $scope.patt = Apform;
        $scope.application = [
            {
                label: 'Name',
                model: 'clientName',
                required: true,
                pattern: $scope.patt.alpha,
                error: 'Valid with letters only',
                type: 'text',
            },
            {
                label: 'Address',
                model: 'clientAddress',
                required: true,
                pattern: $scope.patt.alpha_numeric,
                error: 'Valid with letters and numbers only',
                type: 'text',
            },
    ....
    ];}};});

该对象在 ngForm 内的ngRepeat中使用:

<ng-form name="applicationForm">
        <div ng-repeat="form in application" class="row">
            <div ng-if="form.type==='text'
                 class="small-8">
                <div class="row input-wrapper">
                    <div class="small-8">
                        <label for="{{form.label}}"
                               class="left inline">{{form.label}}
                        </label>
                    </div>
                    <div class="small-3 colums">
                        <input type="{{form.type}}"
                               id="{{form.label}}"
                               name="{{form.label}}"
                               placeholder="{{form.label}}"
                               ng-required="{{form.required}}"
                               ng-pattern="'{{form.pattern}}'"
                               ng-model="$parent.$parent.output[form.model]">
                    </div>
                    <small>
                        applicationForm[{{form.label}}].$valid: {{applicationForm[form.label].$valid}}<br>
                        applicationForm[{{form.label}}].$error: {{applicationForm[form.label].$error}}<br>
                        applicationForm[{{form.label}}].required: {{applicationForm[form.label].required}}<br>
                        applicationForm.$valid: {{applicationForm.$valid}}
                    </small>
                </div>
            </div>
</ng-form>

怎么了?

4

0 回答 0