为什么我的NgMessages表单验证行为不合适?

时间:2016-07-07 09:45:49

标签: javascript angularjs

我编写了一个使用Angular JS表单验证的代码。

有两种形式, 第一个使用表单验证

和第二次使用ng-messages,

问题是,第二种形式似乎不是第一种形式。

在第一种形式中,有3个文本字段,

结束字段已被要求。

因此,一旦我加载页面并尝试提交表单而不填写它, 错误消息显示在各个字段中。

现在,如果文本字段不为空但有一些数字验证, 如果我输入文本,则只显示数字验证消息。

但对于上面提到的验证行为,我无法使用ng-Messages以第二种形式实现。

代码如下,

<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
    <title>Validator Examples</title>
    <script type="text/javascript" src="/home/rahul/Installers/jquery-3.0.0.js"></script>   
    <script type="text/javascript" src="/home/rahul/Installers/angular.js"></script>
    <script type="text/javascript" src="/home/rahul/Installers/Bootstrapv3.0.2/js/bootstrap.js"></script>

    <link rel="stylesheet" href="/home/rahul/Installers/Bootstrapv3.0.2/css/bootstrap.css">
    <link rel="stylesheet" href="/home/rahul/Installers/Bootstrapv3.0.2/css/bootstrap-theme.css">


    <script type="text/javascript">
        angular.module("myApp",[]);
        angular.module("myApp").controller("myCtrl",myCtrl);
        function myCtrl(){
            var vm = this;
            vm.formOne = {}
            vm.formOne.name = "";
            vm.formOne.address = "";                
            vm.formOne.age = 0;

            vm.formTwo = {}
            vm.formTwo.name = "";
            vm.formTwo.address = "";                
            vm.formTwo.age = 0;
        }
    </script>
    <style type="text/css">
        .form-error{
            color : red;
        }
    </style>
</head>
<body ng-controller="myCtrl as vm">
    <div class="container">
        <div class="page-header">
            <h3>Validator Examples</h3>
        </div>
        <div class="row">
            <div class="col-md-4 col-md-offset-2">
                <div class="page-header">
                    <h5><b>ngForm Validator Examples</b></h5>
                </div>
                <form name="formOne" novalidate>
                    <div class="form-group">
                        <input type="text" class="form-control" name="name1" placeholder="Name" required ng-model="vm.formOne.name"
                         minlength="5" maxlength="10" />
                        <div ng-show="formOne.$submitted || formOne.name1.$touched">
                            <div ng-show="formOne.name1.$error.required" class="form-error">Name can't be empty</div>
                            <div ng-show="formOne.name1.$error.minlength" class="form-error">Name can't be less than 5</div>
                            <div ng-show="formOne.name1.$error.maxlength" class="form-error">Name can't be more than 10</div>
                        </div>  
                    </div>
                    <div class="form-group">
                        <input type="text" class="form-control" name="address1" placeholder="Address" ng-model="vm.formOne.address"
                         required />
                        <div ng-show="formOne.$submitted || formOne.address1.$touched">
                            <div ng-show="formOne.address1.$error.required" class="form-error">Address can't be empty</div>
                        </div>
                    </div>
                    <div class="form-group">
                        <input type="number" class="form-control" name="age1" placeholder="Age" ng-model="vm.formOne.age"
                         required number/>
                        <div ng-show="formOne.$submitted || formOne.age1.$touched">
                            <div ng-show="formOne.age1.$error.required" class="form-error">Age can't be empty</div>
                            <div ng-show="formOne.age1.$error.number" class="form-error">Age should be numeric</div>
                        </div>
                    </div>
                    <button type="submit" class="btn btn-primary">Submit</button>
                </form>
            </div>
        </div><!-- row -->
        <div class="row">
            <div class="col-md-4 col-md-offset-2">
                <div class="page-header">
                    <h5><b>ngForm ngMessages Validator Examples</b></h5>
                </div>
                <form name="formTwo" novalidate>
                    <div class="form-group">
                        <input type="text" class="form-control" name="name1" placeholder="Name"
                        ng-model="vm.formTwo.name" required ng-minlength="5" ng-maxlength="10" />
                        <div ng-messages="formTwo.name1.$error" 
                        ng-show="formTwo.$submitted || formTwo.name1.$touched" class="form-error" role="alert">
                            <div ng-message="required">Name can't be empty</div>
                            <div ng-message="minlength">Name can't be less than 5</div>
                            <div ng-message="maxlength">Name can't be more than 10</div>                
                        </div>  
                    </div>
                    <div class="form-group">
                        <input type="text" class="form-control" name="address1" placeholder="Address" ng-model="vm.formTwo.address"
                         required  />
                        <div ng-messages="formTwo.address1.$error"
                         ng-show="formTwo.$submitted || formTwo.address1.$touched" class="form-error">
                            <div ng-message="required">Name can't be empty</div>                                
                        </div>
                    </div>
                    <div class="form-group">
                        <input type="number" class="form-control" name="age1" placeholder="Age" ng-model="vm.formTwo.age"
                         required number />
                        <div ng-messages="formTwo.age1.$error" ng-show="formTwo.$submitted || formTwo.age1.$touched"
                          class="form-error">
                            <div ng-message="required">Age can't be empty</div>
                            <div ng-message="number">Age should be numeric</div>
                        </div>
                    </div>
                    <button type="submit" class="btn btn-primary">Submit</button>
                </form>
            </div>
        </div><!-- row -->
    </div>
</body>     

让我告诉你比较,

在表单一中,第一个文件验证就像

<form name="formOne" novalidate>
                    <div class="form-group">
                        <input type="text" class="form-control" name="name1" placeholder="Name" required ng-model="vm.formOne.name"
                         minlength="5" maxlength="10" />
                        <div ng-show="formOne.$submitted || formOne.name1.$touched">
                            <div ng-show="formOne.name1.$error.required" class="form-error">Name can't be empty</div>
                            <div ng-show="formOne.name1.$error.minlength" class="form-error">Name can't be less than 5</div>
                            <div ng-show="formOne.name1.$error.maxlength" class="form-error">Name can't be more than 10</div>
                        </div>  
                    </div>

在表格二中,验证就像

<form name="formTwo" novalidate>
                    <div class="form-group">
                        <input type="text" class="form-control" name="name1" placeholder="Name"
                        ng-model="vm.formTwo.name" required ng-minlength="5" ng-maxlength="10" />
                        <div ng-messages="formTwo.name1.$error" 
                        ng-show="formTwo.$submitted || formTwo.name1.$touched" class="form-error" role="alert">
                            <div ng-message="required">Name can't be empty</div>
                            <div ng-message="minlength">Name can't be less than 5</div>
                            <div ng-message="maxlength">Name can't be more than 10</div>                
                        </div>  

在表单一中,我只能看到那些无效的消息,

但是在表单2中,我看到该字段中的所有错误消息。

您还可以在此处查看我的工作代码,

FormValidation

请告诉我,我出错了吗

1 个答案:

答案 0 :(得分:0)

  • 确保您在<script>

  • 中添加了 angular-message.js 文件
  • 比在下面的模块中注入ngMessages

angular.module('myApp', ['ngMessages']);

  • 使用form="vm.formTwo"ng-messages="vm.formTwo....
  • 编写控制器别名

这是working example