AngularJS form: formName.$valid 始终为真
AngularJS form: formName.$valid is always been true
我的 angular 应用程序中有一个表单,html 代码如下,但是 formName.$valid 始终为真,即使输入字段为空也是如此。
<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>
有人可以帮我解决这个问题吗?
您没有对任何元素设置验证,因此表单始终有效。如果你想让一个字段不为空,你可以这样写:
<input type="text" name="contactName" class="form-control" placeholder="Name*" ng-model="formData.contactName" required>
我的 angular 应用程序中有一个表单,html 代码如下,但是 formName.$valid 始终为真,即使输入字段为空也是如此。
<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>
有人可以帮我解决这个问题吗?
您没有对任何元素设置验证,因此表单始终有效。如果你想让一个字段不为空,你可以这样写:
<input type="text" name="contactName" class="form-control" placeholder="Name*" ng-model="formData.contactName" required>