Jus*_*oel 4 forms validation angularjs
SO上有很多类似的问题.我经历了很多,但没有发现我的问题.
我有一个正在使用的表单ng-submit
.不幸的是,即使表单无效,按Enter或单击提交仍然有效.该ng-submit
方法被调用.
CodePen示例:http://codepen.io/calendee/pen/tgFhq
<form name="testForm" novalidate ng-submit="submit()">
<label for="firstname">First Name:</label>
<input type="text" name="firstname" ng-model="data.firstname" required="true" ng-minlength="4" ng-maxlength="10">
<div class="errors">
<p>firstname Errors:</p>
<p ng-show="testForm.firstname.$error.required">firstname is required</p>
<p ng-show="testForm.firstname.$error.minlength">firstname is too short</p>
<p ng-show="testForm.firstname.$error.maxlength">firstname is too long</p>
</div>
<label for="lastname">Last Name:</label>
<input type="text" name="lastname" ng-model="data.lastname" ng-required="true" ng-minlength="4" ng-maxlength="10">
<div class="errors">
<p>lastname Errors:</p>
<p ng-show="testForm.lastname.$error.required">lastname is required</p>
<p ng-show="testForm.lastname.$error.minlength">lastname is too short</p>
<p ng-show="testForm.lastname.$error.maxlength">lastname is too long</p>
</div>
<button class="button button-balanced" type="submit">Submit</button>
</form>
Run Code Online (Sandbox Code Playgroud)
如果我更改表单,ng-form
即使表单有效,提交事件也根本不起作用.
CodePen示例:http://codepen.io/calendee/pen/imJdc
<ng-form name="testForm" novalidate ng-submit="submit()">
<label for="firstname">First Name:</label>
<input type="text" name="firstname" ng-model="data.firstname" required="true" ng-minlength="4" ng-maxlength="10">
<div class="errors">
<p>firstname Errors:</p>
<p ng-show="testForm.firstname.$error.required">firstname is required</p>
<p ng-show="testForm.firstname.$error.minlength">firstname is too short</p>
<p ng-show="testForm.firstname.$error.maxlength">firstname is too long</p>
</div>
<label for="lastname">Last Name:</label>
<input type="text" name="lastname" ng-model="data.lastname" ng-required="true" ng-minlength="4" ng-maxlength="10">
<div class="errors">
<p>lastname Errors:</p>
<p ng-show="testForm.lastname.$error.required">lastname is required</p>
<p ng-show="testForm.lastname.$error.minlength">lastname is too short</p>
<p ng-show="testForm.lastname.$error.maxlength">lastname is too long</p>
</div>
<button class="button button-balanced" type="submit">Submit</button>
</ng-form>
Run Code Online (Sandbox Code Playgroud)
有没有人对我在这里做错了什么有建议?
小智 7
您可以使用内置属性$valid
:
<form name="testForm" novalidate ng-submit="testForm.$valid && submit()">
Run Code Online (Sandbox Code Playgroud)
只有在testForm.$valid
为true 时才调用submit函数.
我在本教程中学到了它:https://www.codeschool.com/courses/shaping-up-with-angular-js.
归档时间: |
|
查看次数: |
2442 次 |
最近记录: |