欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

AngularJS表單詳解及示例代碼

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

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

事件

AngularJS提供可與HTML控件相關聯的多個事件。例如ng-click通常與按鈕相關聯。以下是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

使用點擊一個按鈕的指令,表單重置數據。

<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>

驗證數據

可使用下面跟蹤誤差。

$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??吹浇Y果如下。

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

相關文章

  • AngularJS指令用法詳解

    AngularJS指令用法詳解

    這篇文章主要介紹了AngularJS指令用法,較為詳細的分析了AngularJS指令的功能、用法及自定義指令的相關實現技巧,需要的朋友可以參考下
    2016-11-11
  • Angular實踐之將Input與Lifecycle轉換成流示例詳解

    Angular實踐之將Input與Lifecycle轉換成流示例詳解

    這篇文章主要為大家介紹了Angular實踐之將Input與Lifecycle轉換成流示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • 解決nodejs中使用http請求返回值為html時亂碼的問題

    解決nodejs中使用http請求返回值為html時亂碼的問題

    下面小編就為大家?guī)硪黄鉀Qnodejs中使用http請求返回值為html時亂碼的問題。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • angular實現IM聊天圖片發(fā)送實例

    angular實現IM聊天圖片發(fā)送實例

    本篇文章主要介紹了angular實現IM聊天圖片發(fā)送實例,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • AngularJS 執(zhí)行流程詳細介紹

    AngularJS 執(zhí)行流程詳細介紹

    本文詳細介紹了從頁面開始加載到頁面渲染完畢過程中,AngularJS執(zhí)行了哪些操作,有需要的小伙伴可以參考下
    2016-08-08
  • Angularjs使用指令做表單校驗的方法

    Angularjs使用指令做表單校驗的方法

    本篇文章主要介紹了Angularjs使用指令做表單校驗的方法,詳細的介紹了用指令做校驗的方法,具有一定的參考價值,有興趣的可以了解一下。
    2017-03-03
  • Angular自定義組件實現數據雙向數據綁定的實例

    Angular自定義組件實現數據雙向數據綁定的實例

    下面小編就為大家分享一篇Angular自定義組件實現數據雙向數據綁定的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • Angular2使用jQuery的方法教程

    Angular2使用jQuery的方法教程

    這篇文章主要給大家介紹了關于Angular2使用jQuery的方法教程,文中介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面來跟著小編一起學習學習吧。
    2017-05-05
  • 淺談angularjs依賴服務注入寫法的注意點

    淺談angularjs依賴服務注入寫法的注意點

    下面小編就為大家?guī)硪黄獪\談angularjs依賴服務注入寫法的注意點。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • Angular 2 利用Router事件和Title實現動態(tài)頁面標題的方法

    Angular 2 利用Router事件和Title實現動態(tài)頁面標題的方法

    本篇文章主要介紹了Angular 2 利用Router事件和Title實現動態(tài)頁面標題的方法,具有一定的參考價值,有興趣的可以了解一下
    2017-08-08

最新評論