6

请看一看。

http://plnkr.co/edit/DuTFYbLVbPkCIvRznYjG?p=preview

其中 ng-pattern regEx 不会应用于输入文本字段。

只有正确应用所需的验证。

HTML:

<body ng-controller="tableController">
    <form name="test">
        <div ng-repeat="model in models">
            <span ng-bind="model.caption"></span>
            <div ng-form name="frm{{$index}}">
                <input name="input{{$index}}"
                    ng-model="data[model.name]"
                    ng-disabled="model.isDisabled"
                    minlength="{{model.minlength}}"
                    maxlength="{{model.maxlength}}"
                    ng-show="model.isShow"
                    ng-pattern="model.pattern"
                    ng-required="true" />
                <br />
                {{data[model.name]}}
            </div>
        </div>
    </form>
    <br />
    <br />
</body>

JS:

angular.module("app", []).controller("tableController", function ($scope) {
        $scope.regEx = "/^\\d+$/";
        $scope.data = {};
        $scope.data.one = 234;
        $scope.data.two = 32432;
        $scope.models = [
            { name: "one", caption: "cOne", isDisabled: false, isShow: true, minlength: 2, maxlength: 10, pattern: "/^\d+$/" },
            { name: "two", caption: "cTwo", isDisabled: false, isShow: true, minlength: 2, maxlength: 5, pattern: "/^\d+$/" },
        ];
            });

有什么建议吗??

-谢谢

4

1 回答 1

12

将正则表达式分配更改为以下内容。因为它应该是一个正则表达式。如果您在双引号中提及,则它变成一个字符串。

$scope.regEx = /^\d+$/;
于 2014-03-24T14:42:36.790 回答