最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

AngularJS表單詳解及示例代碼

 更新時間:2016年08月17日 09:21:51   作者:taichiman_cn  
本文主要介紹AngularJS表單的知識,這里整理了表單的基礎(chǔ)知識和簡單的示例代碼,有需要的小伙伴可以參考下

AngularJS提供豐富填寫表單和驗證。我們可以用ng-click來處理AngularJS點擊按鈕事件,然后使用 $dirty 和 $invalid標(biāo)志做驗證的方式。使用novalidate表單聲明禁止任何瀏覽器特定的驗證。表單控件使用了大量的角活動。讓我們快速瀏覽一下有關(guān)事件先。

事件

AngularJS提供可與HTML控件相關(guān)聯(lián)的多個事件。例如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

使用點擊一個按鈕的指令,表單重置數(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>

驗證數(shù)據(jù)

可使用下面跟蹤誤差。

$dirty - 規(guī)定值已被改變。

$invalid- 該值的狀態(tài)是無效的。

$error- 指出確切的錯誤。

例子

下面的例子將展示上述所有指令。

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瀏覽器打開textAngularJS.html。看到結(jié)果如下。

以上就是AngularJS表單的知識整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)知識,謝謝大家對本站的支持!

相關(guān)文章

最新評論

奎屯市| 澎湖县| 吴堡县| 仁布县| 城市| 奉贤区| 鄂温| 宽城| 游戏| 邹城市| 天柱县| 静乐县| 宜春市| 固安县| 凌云县| 贡嘎县| 宕昌县| 普洱| 河东区| 陇南市| 永寿县| 象山县| 沂源县| 新邵县| 麻城市| 广德县| 黄平县| 梧州市| 辽阳市| 资溪县| 东明县| 秦皇岛市| 什邡市| 龙南县| 闸北区| 将乐县| 怀远县| 门源| 简阳市| 宜宾市| 洱源县|