激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服務(wù)器之家 - 編程語言 - JavaScript - angularjs - 教你用AngularJS框架一行JS代碼實現(xiàn)控件驗證效果

教你用AngularJS框架一行JS代碼實現(xiàn)控件驗證效果

2022-01-03 22:07AngularJS教程網(wǎng) angularjs

簡單來說Angular.js是google開發(fā)者設(shè)計和開發(fā)的一套前端開發(fā)框架,幫助你簡化前端開發(fā)的負(fù)擔(dān)。到底能簡化到什么程度呢,今天我們來看下,一行代碼實現(xiàn)控件驗證效果,有木有嚇尿?

教你用AngularJS框架一行JS代碼實現(xiàn)控件驗證效果

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

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

HTML。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!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">
  <!-- 主要內(nèi)容區(qū)域 -->
  <div class="row main-content">
   <div class="col-md-9">
 
    <!-- 聯(lián)系我們表單區(qū)域 -->
    <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 內(nèi)容 -->
     <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">內(nèi)容*</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="發(fā)布" 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

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

這樣就好啦。幾個關(guān)鍵的地方解釋一下:

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

延伸 · 閱讀

精彩推薦
  • angularjsangularjs實現(xiàn)與服務(wù)器交互分享

    angularjs實現(xiàn)與服務(wù)器交互分享

    AngularJS是Google開發(fā)的純客戶端JavaScript技術(shù)的WEB框架,用于擴展、增強HTML功能,它專為構(gòu)建強大的WEB應(yīng)用而設(shè)計。...

    angularjs教程網(wǎng)7312022-01-03
  • angularjs教你用AngularJS框架一行JS代碼實現(xiàn)控件驗證效果

    教你用AngularJS框架一行JS代碼實現(xiàn)控件驗證效果

    簡單來說Angular.js是google開發(fā)者設(shè)計和開發(fā)的一套前端開發(fā)框架,幫助你簡化前端開發(fā)的負(fù)擔(dān)。到底能簡化到什么程度呢,今天我們來看下,一行代碼實現(xiàn)控...

    AngularJS教程網(wǎng)8012022-01-03
  • angularjsAngularJS 中括號的作用詳解

    AngularJS 中括號的作用詳解

    這篇文章主要介紹了AngularJS 中括號的作用,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下...

    Java Pro8652022-02-22
  • angularjsAngular.JS中指令的命名規(guī)則詳解

    Angular.JS中指令的命名規(guī)則詳解

    這篇文章主要給大家介紹了關(guān)于Angular.JS中指令命名規(guī)則的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看...

    劉小光10402022-01-17
  • angularjsangularJS提交表單(form)

    angularJS提交表單(form)

    這篇文章主要介紹了angularJS提交表單(form)的方法和示例,需要的朋友可以參考下...

    angularJS教程網(wǎng)3972022-01-03
  • angularjsangularJS 中input示例分享

    angularJS 中input示例分享

    這篇文章主要介紹了angularJS 中input示例分享,需要的朋友可以參考下...

    angularJS教程網(wǎng)5652022-01-03
  • angularjsAngular框架詳解之視圖抽象定義

    Angular框架詳解之視圖抽象定義

    這篇文章主要給大家介紹了關(guān)于Angular框架詳解之視圖抽象定義的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)...

    被刪6782022-02-24
  • angularjsangularJS中router的使用指南

    angularJS中router的使用指南

    這篇文章主要介紹了angularJS中router的使用方法和示例分享,需要的朋友可以參考下...

    angularJS教程網(wǎng)8372022-01-03
主站蜘蛛池模板: av最新在线观看 | 高清久久久| 一级片久久免费 | 成人午夜免费看 | 成年免费看 | 国产精品免费av一区二区三区 | 久久17| 草草视频在线播放 | 99久久免费看精品国产一区 | 黄色片网站在线看 | 好吊色37pao在线观看 | 国产日产精品久久久久快鸭 | 精品国产91久久久久久 | 久久久久九九九女人毛片 | 一本一道久久久a久久久精品91 | 亚洲人片在线观看 | 香蕉久草视频 | 九草视频| 日本高清电影在线播放 | 99爱视频在线观看 | 精品国产一区二 | 国产流白浆高潮在线观看 | 精品国产一区二区三区在线 | 欧美囗交 | 精品国产一区二区三区四区阿崩 | 在线观看视频日本 | 在线成人www免费观看视频 | 久久99亚洲精品久久99果 | 羞羞视频免费网站日本动漫 | 欧洲黄视频 | 精品国产亚洲人成在线 | 国产免费一级大片 | 在线区| 日本不卡一区二区三区在线 | 日韩欧美电影在线观看 | 国产一区二区精彩视频 | 欧美一级毛片免费观看视频 | 成人在线观看免费视频 | 毛片在哪里看 | 美女色影院 | 羞羞网站视频 |