基于AngularJS實現(xiàn)表單驗證功能
更新時間:2017年07月28日 14:53:39 作者:不羈放縱愛自由098112
這篇文章主要為大家詳細介紹了基于AngularJS實現(xiàn)表單驗證功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了AngularJS實現(xiàn)表單驗證功能的具體代碼,供大家參考,具體內(nèi)容如下
<!--實例解析
ng-app 指令定義了 AngularJS 應(yīng)用。
ng-controller 指令定義了應(yīng)用控制器。
ng-model 指令綁定了兩個 input 元素到模型的 user 對象。
formCtrl 函數(shù)設(shè)置了 master 對象的初始值,并定義了 reset() 方法。
reset() 方法設(shè)置了 user 對象等于 master 對象。
ng-click 指令調(diào)用了 reset() 方法,且在點擊按鈕時調(diào)用。
novalidate 屬性在應(yīng)用中不是必須的,但是你需要在 AngularJS 表單中使用,用于重寫標(biāo)準的 HTML5 驗證。 -->
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<meta charset="utf-8">
<script src="js/angular.js"></script>
</head>
<body>
<!-- Checkbox(單選框)
我們可以使用 ng-model 來綁定單選按鈕到你的應(yīng)用中。
單選框使用同一個 ng-model ,可以有不同的值,但只有被選中的單選按鈕的值會被使用
-->
<form>
選擇一個選項:
<input type="radio" ng-model="myVar" value="dogs">Dogs
<input type="radio" ng-model="myVar" value="tuts">Tutorials
<input type="radio" ng-model="myVar" value="cars">Cars
</form>
<div ng-switch="myVar">
<div ng-switch-when="dogs">
<h1>Dogs</h1>
<p>Welcome to a world of dogs.</p>
</div>
<div ng-switch-when="tuts">
<h1>Tutorials</h1>
<p>Learn from examples.</p>
</div>
<div ng-switch-when="cars">
<h1>Cars</h1>
<p>Read about cars.</p>
</div>
</div>
<p>ng-switch 指令根據(jù)單選按鈕的選擇結(jié)果顯示或隱藏 HTML 區(qū)域。</p><br><br><br><br>
<!-- 下拉菜單
使用 ng-model 指令可以將下拉菜單綁定到你的應(yīng)用中。
ng-model 屬性的值為你在下拉菜單選中的選項
-->
<form>
選擇一個選項:
<select ng-model="myVar2">
<option value="">
<option value="dogs">Dogs
<option value="tuts">Tutorials
<option value="cars">Cars
</select>
</form>
<div ng-switch="myVar2">
<div ng-switch-when="dogs">
<h1>Dogs</h1>
<p>Welcome to a world of dogs.</p>
</div>
<div ng-switch-when="tuts">
<h1>Tutorials</h1>
<p>Learn from examples.</p>
</div>
<div ng-switch-when="cars">
<h1>Cars</h1>
<p>Read about cars.</p>
</div>
</div>
<p>ng-switch 指令根據(jù)下拉菜單的選擇結(jié)果顯示或隱藏 HTML 區(qū)域。</p><br><br><br><br>
<!-- novalidate-->
<form action="xxx.do" novalidate>
E-mail: <input type="email" name="user_email">
<input type="submit">
</form>
<p><strong>注意:</strong>在 Safari 和 Internet Explorer 9 及之前的版本中不支持 novalidate 屬性。這個屬性可以關(guān)閉瀏覽器默認校驗</p><br><br><br><br>
<!--Checkbox(復(fù)選框)
checkbox 的值為 true 或 false,可以使用 ng-model 指令綁定,它的值可以用于應(yīng)用中-->
<div ng-app="">
<form>
選中復(fù)選框,顯示標(biāo)題:
<input type="checkbox" ng-model="myVar">
</form>
<h1 ng-show="myVar">My Header</h1>
</div>
<p>標(biāo)題使用了 ng-show 指令,復(fù)選框選中后顯示 h1 標(biāo)簽內(nèi)容。</p><br><br><br><br>
<!-- HTML 表單
HTML 表單通常與 HTML 控件同時存在
以下 HTML input 元素被稱為 HTML 控件:
input 元素
select 元素
button 元素
textarea 元素
-->
<div ng-app="myApp" ng-controller="formCtrl">
<form novalidate>
First Name:<br>
<input type="text" ng-model="user.firstName"><br>
Last Name:<br>
<input type="text" ng-model="user.lastName">
<br><br>
<button ng-click="reset()">RESET</button>
</form>
<p>form = {{user}}</p>
<p>master = {{master}}</p>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('formCtrl', function($scope) {
$scope.master = {firstName: "John", lastName: "Doe"};
$scope.reset = function() {
$scope.user = angular.copy($scope.master);
};
$scope.reset();
});
</script>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談關(guān)于angularJs中使用$.ajax的注意點
本篇文章主要介紹了關(guān)于angularJs中使用$.ajax的注意點,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
angularjs學(xué)習(xí)筆記之雙向數(shù)據(jù)綁定
AngularJS在$scope變量中使用臟值檢查來實現(xiàn)了數(shù)據(jù)雙向綁定。和Ember.js數(shù)據(jù)雙向綁定中動態(tài)設(shè)施setter和getter不同,臟治檢查允許AngularJS監(jiān)視那些存在或者不存在的變量。2015-09-09
快速學(xué)習(xí)AngularJs HTTP響應(yīng)攔截器
任何時候,如果我們想要為請求添加全局功能,例如身份認證、錯誤處理等,在請求發(fā)送給服務(wù)器之前或服務(wù)器返回時對其進行攔截,是比較好的實現(xiàn)手段2015-12-12
Angular中ng-repeat與ul li的多層嵌套重復(fù)問題
這篇文章主要介紹了Angular中ng-repeat與ul li的多層嵌套重復(fù)問題,需要的朋友可以參考下2017-07-07
angular學(xué)習(xí)之ngRoute路由機制
這篇文章主要介紹了angular學(xué)習(xí)之ngRoute路由機制,ngRoute是一個Module,提供路由和深層鏈接所需的服務(wù)和指令。有需要的可以了解一下。2017-04-04
AngularJS基礎(chǔ) ng-mousemove 指令簡單示例
本文主要介紹AngularJS ng-mousemove 指令,這里幫大家整理了ng-mousemove 指令的詳細資料,并附有示例代碼,有需要的朋友參考下2016-08-08
詳解angular部署到iis出現(xiàn)404解決方案
這篇文章主要介紹了詳解angular部署到iis出現(xiàn)404解決方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08

