简体   繁体   English

angularJS ng-pattern无效

[英]angularJS ng-pattern not working

Please take a look plunker. 请看看plunker。

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

where ng-pattern regEx is not going to apply on input text field. 其中ng-pattern regEx不适用于输入文本字段。

where only required validation is applying properly. 只需要正确应用所需验证的地方。

HTML: 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: 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+$/" },
        ];
            });

Any suggestion ?? 有什么建议??

-Thanks -谢谢

Change the regular expression assignment to the below. 将正则表达式分配更改为以下。 Since it should be a regular expression. 因为它应该是正则表达式。 If you mention in double quotes then it becomes a string. 如果你用双引号提到那么它就变成了一个字符串。

$scope.regEx = /^\d+$/;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM