ngPattern拒绝所有输入,在ngForm

时间:2015-08-19 22:58:44

标签: angularjs angularjs-ng-form ng-pattern

我有一个ngRepeat填充表单但是form.input。$ error.pattern为true(输入与RegExp不匹配)与第一个输入的文本输入中的任何内容。对于form.input之后的所有输入,$ error.required保持为true,即使输入文本中存在某些内容。类.ng-pristine,.ng-touching,.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;

注入控制器:

controller:function($ scope,Apform)         {

        $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>

出了什么问题?

0 个答案:

没有答案