AngularJS表單詳解及示例代碼
AngularJS提供豐富填寫(xiě)表單和驗(yàn)證。我們可以用ng-click來(lái)處理AngularJS點(diǎn)擊按鈕事件,然后使用 $dirty 和 $invalid標(biāo)志做驗(yàn)證的方式。使用novalidate表單聲明禁止任何瀏覽器特定的驗(yàn)證。表單控件使用了大量的角活動(dòng)。讓我們快速瀏覽一下有關(guān)事件先。
事件
AngularJS提供可與HTML控件相關(guān)聯(lián)的多個(gè)事件。例如ng-click通常與按鈕相關(guān)聯(lián)。以下是AngularJS支持的事件。
ng-click
ng-dbl-click
ng-mousedown
ng-mouseup
ng-mouseenter
ng-mouseleave
ng-mousemove
ng-mouseover
ng-keydown
ng-keyup
ng-keypress
ng-change
ng-click
使用點(diǎn)擊一個(gè)按鈕的指令,表單重置數(shù)據(jù)。
<input name="firstname" type="text" ng-model="firstName" required>
<input name="lastname" type="text" ng-model="lastName" required>
<input name="email" type="email" ng-model="email" required>
<button ng-click="reset()">Reset</button>
<script>
function studentController($scope) {
$scope.reset = function(){
$scope.firstName = "Mahesh";
$scope.lastName = "Parashar";
$scope.email = "MaheshParashar@yiibai.com";
}
$scope.reset();
}
</script>
驗(yàn)證數(shù)據(jù)
可使用下面跟蹤誤差。
$dirty - 規(guī)定值已被改變。
$invalid- 該值的狀態(tài)是無(wú)效的。
$error- 指出確切的錯(cuò)誤。
例子
下面的例子將展示上述所有指令。
testAngularJS.html
<html>
<head>
<title>Angular JS Forms</title>
<style>
table, th , td {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
}
table tr:nth-child(odd) {
background-color: #f2f2f2;
}
table tr:nth-child(even) {
background-color: #ffffff;
}
</style>
</head>
<body>
<h2>AngularJS Sample Application</h2>
<div ng-app="" ng-controller="studentController">
<form name="studentForm" novalidate>
<table border="0">
<tr><td>Enter first name:</td><td><input name="firstname" type="text" ng-model="firstName" required>
<span style="color:red" ng-show="studentForm.firstname.$dirty && studentForm.firstname.$invalid">
<span ng-show="studentForm.firstname.$error.required">First Name is required.</span>
</span>
</td></tr>
<tr><td>Enter last name: </td><td><input name="lastname" type="text" ng-model="lastName" required>
<span style="color:red" ng-show="studentForm.lastname.$dirty && studentForm.lastname.$invalid">
<span ng-show="studentForm.lastname.$error.required">Last Name is required.</span>
</span>
</td></tr>
<tr><td>Email: </td><td><input name="email" type="email" ng-model="email" length="100" required>
<span style="color:red" ng-show="studentForm.email.$dirty && studentForm.email.$invalid">
<span ng-show="studentForm.email.$error.required">Email is required.</span>
<span ng-show="studentForm.email.$error.email">Invalid email address.</span>
</span>
</td></tr>
<tr><td><button ng-click="reset()">Reset</button></td><td><button
ng-disabled="studentForm.firstname.$dirty && studentForm.firstname.$invalid ||
studentForm.lastname.$dirty && studentForm.lastname.$invalid ||
studentForm.email.$dirty && studentForm.email.$invalid"
ng-click="submit()">Submit</button></td></tr>
</table>
</form>
</div>
<script>
function studentController($scope) {
$scope.reset = function(){
$scope.firstName = "Mahesh";
$scope.lastName = "Parashar";
$scope.email = "MaheshParashar@yiibai.com";
}
$scope.reset();
}
</script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
</body>
</html>
輸出
在Web瀏覽器打開(kāi)textAngularJS.html??吹浇Y(jié)果如下。

以上就是AngularJS表單的知識(shí)整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)知識(shí),謝謝大家對(duì)本站的支持!
相關(guān)文章
Angularjs Ng_repeat中實(shí)現(xiàn)復(fù)選框選中并顯示不同的樣式方法
今天小編就為大家分享一篇Angularjs Ng_repeat中實(shí)現(xiàn)復(fù)選框選中并顯示不同的樣式方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
angularjs實(shí)現(xiàn)多選框分段全選效果實(shí)現(xiàn)
這篇文章主要為大家介紹了angularjs實(shí)現(xiàn)多選框分段全選效果實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
angular2中使用第三方j(luò)s庫(kù)的實(shí)例
下面小編就為大家分享一篇angular2中使用第三方j(luò)s庫(kù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
Angular CLI在Angular項(xiàng)目中如何使用scss詳解
angular-cli自身支持Scss預(yù)處理器,Scss比css更加方便靈活,而且層次清晰,代碼整潔。下面這篇文章主要給大家介紹了關(guān)于Angular CLI在Angular項(xiàng)目中如何使用scss的相關(guān)資料,需要的朋友可以參考下。2018-04-04
AngularJS 中的Promise --- $q服務(wù)詳解
這篇文章主要介紹了AngularJS 中的Promise --- $q服務(wù)詳解方法的相關(guān)資料,需要的朋友可以參考下2016-09-09
基于?angular?material?theming?機(jī)制修改?mat-toolbar?的背景色(示例詳解
最近在學(xué)習(xí)?angular,記錄一下昨天的進(jìn)展,解決的問(wèn)題是通過(guò)?theme?的配置修改?mat-toolbar?的背景色,避免對(duì)色彩的硬編碼,這篇文章主要介紹了基于?angular?material?theming?機(jī)制修改?mat-toolbar?的背景色,需要的朋友可以參考下2022-10-10
angularjs中控制視圖的控制器的兩種注入依賴項(xiàng)及服務(wù)的寫(xiě)法小結(jié)
在AngularJS中,控制器的依賴注入有兩種方法:顯式依賴注入和隱匿依賴注入,顯式依賴注入通過(guò)使用字符串?dāng)?shù)組形式來(lái)注入依賴項(xiàng),本文給大家介紹angularjs中控制視圖的控制器的兩種注入依賴項(xiàng)及服務(wù)的寫(xiě)法,感興趣的朋友一起看看吧2024-09-09
AngularJS實(shí)現(xiàn)在ng-Options加上index的解決方法
這篇文章主要介紹了AngularJS實(shí)現(xiàn)在ng-Options加上index的解決方法,結(jié)合實(shí)例形式分析了AngularJS在ngOptions添加索引的操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-11-11
Angular2.js實(shí)現(xiàn)表單驗(yàn)證詳解
這篇文章主要介紹了Angular2.js實(shí)現(xiàn)表單驗(yàn)證的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06

