AngularJS形式:formName。$ valid始终为true

时间:2016-07-14 13:13:42

标签: javascript html angularjs forms ng-messages

我的角度应用程序中有一个表单,html代码如下所示,但是formName。即使输入字段为空,$ valid仍然为true。

<form name="contactForm" novalidate ng-submit="processForm(formData)" autocomplete="off">
            <div id="name-group" class="form-group">
                <input type="text" name="contactName" class="form-control" placeholder="Name*" ng-model="formData.contactName">
                <!--<span class="help-block" ng-show="errorName"></span>-->
            </div>
            <div id="email-group" class="form-group">
                <input type="email" name="contactEmail" class="form-control" placeholder="Email*" ng-model="formData.contactEmail">
                <!--<span class="help-block" ng-show="errorSuperhero"></span>-->
            </div>
            <div id="phone-group" class="form-group">
                <input type="tel" name="contactPhone" class="form-control" placeholder="Phone*" ng-model="formData.contactPhone">
                <!--<span class="help-block" ng-show="errorPhone"></span>-->
            </div>
            <div id="content-group" class="form-group">
                <textarea  rows="5" cols="50" name="contactMessage" class="form-control" placeholder="Message" ng-model="formData.contactMessage"></textarea>
                <!--<span class="help-block" ng-show="errorContent"></span>-->
            </div>
            <div class="btn-block">
                <button type="submit" ng-disabled="contactForm.formData.$invalid" class="btn btn-success btn-lg">
                    <span class="glyphicon glyphicon-share-alt"></span> Submit!
                </button>
                <button ng-disabled="contactForm.formData.$dirty" type="reset" class="btn btn-info btn-lg">
                    <span class="glyphicon glyphicon-refresh"></span> Reset!
                </button>
            </div>
</form>

有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:2)

您没有对任何元素设置验证,因此表单始终有效。如果你想让一个字段非空,你可以写下:

<input type="text" name="contactName" class="form-control" placeholder="Name*" ng-model="formData.contactName" required>