国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

教你用AngularJS框架一行JS代碼實現控件驗證效果

2019-11-20 14:25:21
字體:
來源:轉載
供稿:網友

如上圖所示,我們需要實現如下這些驗證功能:

控件都是必輸控件
都需要控制最大長度
第一次打開頁面,控件不能顯示為錯誤狀態
輸入內容再清空后,必輸控件需要顯示為錯誤狀態
只有所有輸入合法后,發布按鈕才能變為可用狀態
通過AngularJS,我們可以很輕松的實現這些要求,只需要1行JS代碼。UI樣式這里采用的是Bootstrap。先上示例代碼:

HTML。

<!DOCTYPE html><html lang="zh-cn" ng-app="ftitApp"><head> <meta charset="utf-8" /> <title>Demo</title> <link href="/Content/bootstrap.css" rel="stylesheet"/> <script src="/Scripts/angular.js"></script></head><body> <div class="container body-content">  <!-- 主要內容區域 -->  <div class="row main-content">   <div class="col-md-9">    <!-- 聯系我們表單區域 -->    <form action="/Contact/Create" method="post" role="form" name="createContactForm" ng-controller="ContactCreateController">     <!-- UserName 您的稱呼 -->     <div class="form-group has-feedback" ng-class="{'has-success' : !createContactForm.UserName.$pristine && createContactForm.UserName.$valid, 'has-error' : !createContactForm.UserName.$pristine && createContactForm.UserName.$invalid }">      <label for="UserName">您的稱呼*</label>      <input type="text" class="form-control" ng-model="userName" name="UserName" autofocus="" required ng-maxlength=30>      <div ng-show="!createContactForm.UserName.$pristine && createContactForm.UserName.$valid">       <span class="glyphicon glyphicon-ok form-control-feedback"></span>      </div>      <div ng-show="!createContactForm.UserName.$pristine && createContactForm.UserName.$invalid">       <span class="glyphicon glyphicon-remove form-control-feedback"></span>      </div>     </div>     <!-- UserMail 郵箱地址 -->     <div class="form-group has-feedback" ng-class="{'has-success' : !createContactForm.UserMail.$pristine && createContactForm.UserMail.$valid, 'has-error' : !createContactForm.UserMail.$pristine && createContactForm.UserMail.$invalid }">      <label for="UserMail">郵箱地址*</label>      <input type="email" class="form-control" ng-model="userMail" name="UserMail" required ng-maxlength=30>      <div ng-show="!createContactForm.UserMail.$pristine && createContactForm.UserMail.$valid">       <span class="glyphicon glyphicon-ok form-control-feedback"></span>      </div>      <div ng-show="!createContactForm.UserMail.$pristine && createContactForm.UserMail.$invalid">       <span class="glyphicon glyphicon-remove form-control-feedback"></span>      </div>     </div>     <!-- Subject 主題 -->     <div class="form-group has-feedback" ng-class="{'has-success' : !createContactForm.Subject.$pristine && createContactForm.Subject.$valid, 'has-error' : !createContactForm.Subject.$pristine && createContactForm.Subject.$invalid }">      <label for="Subject">主題*</label>      <input type="text" class="form-control" ng-model="subject" name="Subject" required ng-maxlength=100>      <div ng-show="!createContactForm.Subject.$pristine && createContactForm.Subject.$valid">       <span class="glyphicon glyphicon-ok form-control-feedback"></span>      </div>      <div ng-show="!createContactForm.Subject.$pristine && createContactForm.Subject.$invalid">       <span class="glyphicon glyphicon-remove form-control-feedback"></span>      </div>     </div>     <!-- Content 內容 -->     <div class="form-group has-feedback" ng-class="{'has-success' : !createContactForm.Content.$pristine && createContactForm.Content.$valid, 'has-error' : !createContactForm.Content.$pristine && createContactForm.Content.$invalid }">      <label for="Content">內容*</label>      <textarea cols="4" rows="5" class="form-control" ng-model="content" name="Content" required ng-maxlength=1000></textarea>      <div ng-show="!createContactForm.Content.$pristine && createContactForm.Content.$valid">       <span class="glyphicon glyphicon-ok form-control-feedback"></span>      </div>      <div ng-show="!createContactForm.Content.$pristine && createContactForm.Content.$invalid">       <span class="glyphicon glyphicon-remove form-control-feedback"></span>      </div>     </div>     <!-- 提交按鈕 -->     <div class="form-group">      <div ng-show="createContactForm.$valid">       <input type="image" src="/Content/images/comment_publish_button.png" onsubmit="submit();" value="發布" ng-disabled='!createContactForm.$valid' />      </div>      <div ng-show="!createContactForm.$valid">       <img src="/Content/images/invalid_publish_button.png" />      </div>     </div>    </form>   </div>  </div> </div>  <script src="/Scripts/ftit/ContactCreateController.js"></script></body></html>

JS代碼(真的只有一行喲)

ContractCreateController.js

var ftitAppModule = angular.module('ftitApp', []);

這樣就好啦。幾個關鍵的地方解釋一下:

ng-class:這個標簽用來控制class的值。例如ng-class="{'has-success' : !createContactForm.Content.$pristine}的意思就是,如果!createContactForm.Content.$pristine的值為true,class的值就為has-success。
ng-show:控制是否顯示該控件。
createContactForm.$valid:全部驗證通過后,值為true,否則為false
createContactForm.Content.$valid:標識Content控件是否通過驗證,通過為true,否則為false
createContactForm.Content.$pristine:標識Content控件是否從未輸入過。從未輸入為true,否則為false
更細節的技術問題請查看AngularJS的技術文檔。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 萨嘎县| 安吉县| 德安县| 乐平市| 克拉玛依市| 新河县| 广元市| 普兰店市| 银川市| 开鲁县| 宁武县| 淄博市| 靖江市| 临朐县| 宜兰县| 铅山县| 盱眙县| 斗六市| 巴彦县| 富平县| 兰溪市| 万载县| 祁连县| 栖霞市| 泰来县| 武安市| 临夏市| 长治县| 许昌县| 淮北市| 建湖县| 克拉玛依市| 邢台市| 巴南区| 即墨市| 柳江县| 格尔木市| 高碑店市| 九江县| 福州市| 保山市|