AngularJS手動表單驗(yàn)證
所謂手動驗(yàn)證是通過AngularJS表單的屬性來驗(yàn)證,而成為AngularJS表單必須滿足兩個(gè)條件:
1、給form元素加上novalidate="novalidate";
2、給form元素加上name="theForm",
如下:
<!DOCTYPE html> <html lang="en" ng-app="myApp1"> <head> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" href="../node_modules/bootstrap/dist/css/bootstrap.min.css"/> <link rel="stylesheet" href="../css/main.css"/> </head> <body> <nav class="navbar navbar-inverse navbar-fixed-top"> <div class="container"> <div class="navbar-header"> <a href="/" class="navbar-brand">Form Submitting</a> </div> </div> </nav> <div class="container main-content" ng-controller="myCtrl1"> <!--novalidate讓表單不要使用html驗(yàn)證--> <!--theForm變成scope的一個(gè)字段--> <form ng-submit="onSubmit(theForm.$valid)" novalidate="novalidate" name="theForm"> <div class="form-group"> <label for="name">Name</label> <input type="text" class="form-control" id="name" ng-model="formModel.name"/> </div> <div class="form-group" ng-class="{ 'has-error': !theForm.email.$valid && (!theForm.$pristine || theForm.$submitted), 'has-success': theForm.email.$valid && (!theForm.$pristine || theForm.$submitted) }"> <label for="email">Email</label> <input type="email" class="form-control" id="email" ng-model="formModel.email" required="required" name="email"/> <p class="help-block" ng-show="theForm.email.$error.required && (!theForm.$pristine || theForm.$submitted)">必填</p> <p class="help-block" ng-show="theForm.email.$error.email && (!theForm.$pristine || theForm.$submitted)">email格式不正確</p> </div> <div class="form-group"> <label for="username">Username</label> <input type="text" class="form-control" id="username" ng-model="formModel.username"/> </div> <div class="form-group"> <label for="age">Age</label> <input type="number" class="form-control" id="age" ng-model="formModel.age"/> </div> <div class="form-group"> <label for="sex">Sex</label> <select name="sex" id="sex" class="form-control" ng-model="formModel.sex"> <option value="">Please choose</option> <option value="male">Mail</option> <option value="femail">Femail</option> </select> </div> <div class="form-group"> <label for="password">Password</label> <input type="text" class="form-control" id="password" ng-model="formModel.password"/> </div> <div class="form-group"> <button class="btn btn-primary" type="submit">Register</button> </div> <pre> {{theForm | json}} </pre> </form> </div> <script src="../node_modules/angular/angular.min.js"></script> <script src="second.js"></script> </body> </html>
- ● 給form加上novalidate="novalidate"意味著表單將不再使用HTML5驗(yàn)證特性
- ● 給form加上name="theForm"意味著表單的名稱是theForm。如何使用theForm,比如我們驗(yàn)證表單是否被修改過theForm.$submitted
- ● 通過ng-submit提交表單
- ● formModel是$scope中的一個(gè)屬性
- ● 對表單的Email進(jìn)行了手動驗(yàn)證,使用了AngularJS表單的眾多屬性,比如theForm.email.$valid,theForm.$pristine,theForm.$submitted, theForm.email.$error.required,theForm.email.$error.email
- ● 通過<pre>{{theForm | json}}</pre>把AngularJS表單的所有屬性都打印出來
{ "$error": { "required": [ { "$validators": {}, "$asyncValidators": {}, "$parsers": [], "$formatters": [ null ], "$viewChangeListeners": [], "$untouched": true, "$touched": false, "$pristine": true, "$dirty": false, "$valid": false, "$invalid": true, "$error": { "required": true }, "$name": "email", "$options": null } ] }, "$name": "theForm", "$dirty": false, "$pristine": true, "$valid": false, "$invalid": true, "$submitted": false, "email": { "$validators": {}, "$asyncValidators": {}, "$parsers": [], "$formatters": [ null ], "$viewChangeListeners": [], "$untouched": true, "$touched": false, "$pristine": true, "$dirty": false, "$valid": false, "$invalid": true, "$error": { "required": true }, "$name": "email", "$options": null }, "sex": { "$validators": {}, "$asyncValidators": {}, "$parsers": [], "$formatters": [], "$viewChangeListeners": [], "$untouched": true, "$touched": false, "$pristine": true, "$dirty": false, "$valid": true, "$invalid": false, "$error": {}, "$name": "sex", "$options": null } }
以上,凡是有name屬性的input都被顯示在上面。
在second.js文件中定義了module,controller以及提交表單的方法。
var myApp1 = angular.module('myApp1',[]); myApp1.controller('myCtrl1', function($scope, $http){ $scope.formModel = {}; $scope.onSubmit = function(){ $http.post('someurl',$scope.formModel) .success(function(data){ console.log(':)'); }) .error(function(data){ console.log(':('); }); console.log($scope.formModel); }; });
以上的表單驗(yàn)證方式好處是可控性強(qiáng),但相對繁瑣。
以上就是本文的全部內(nèi)容,希望對AngularJS手動表單驗(yàn)證能夠熟練操作。
相關(guān)文章
深入淺析AngularJS中的一次性數(shù)據(jù)綁定 (bindonce)
這篇文章主要介紹了AngularJS中的一次性數(shù)據(jù)綁定 (bindonce)知識,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下2017-05-05在Angular中實(shí)現(xiàn)一個(gè)級聯(lián)效果的下拉框的示例代碼
這篇文章主要介紹了在Angular中實(shí)現(xiàn)一個(gè)級聯(lián)效果的下拉框的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05詳解JavaScript的AngularJS框架中的表達(dá)式與指令
這篇文章主要介紹了JavaScript的AngularJS框架中的表達(dá)式與指令,文中羅列了幾個(gè)常用的指令屬性加以說明,需要的朋友可以參考下2016-03-03Angular?結(jié)合?dygraphs?實(shí)現(xiàn)?annotation功能
這篇文章主要介紹了Angular?結(jié)合?dygraphs?實(shí)現(xiàn)?annotation,本文,我們直接結(jié)合 Angular 來演示,如何通過 dygraphs 實(shí)現(xiàn)折線圖上的 annotation 的功能,需要的朋友可以參考下2022-08-08Angular4實(shí)現(xiàn)圖片上傳預(yù)覽路徑不安全的問題解決
這篇文章主要給大家介紹了關(guān)于Angular4實(shí)現(xiàn)圖片上傳預(yù)覽路徑不安全問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12解決Angular.js中使用Swiper插件不能滑動的問題
下面小編就為大家分享一篇解決Angular.js中使用Swiper插件不能滑動的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02angularjs實(shí)現(xiàn)Tab欄切換效果
這篇文章主要為大家詳細(xì)介紹了angularjs實(shí)現(xiàn)Tab欄切換效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03