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

AngularJS的表單使用詳解

 更新時間:2015年06月17日 10:16:30   投稿:goldensun  
這篇文章主要介紹了AngularJS的表單使用詳解,在JS原有的基礎(chǔ)上提供了更多與HTML交互的功能,需要的朋友可以參考下

 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??吹浇Y(jié)果如下。

2015617100230750.png (715×347)

相關(guān)文章

  • angular 服務(wù)隨記小結(jié)

    angular 服務(wù)隨記小結(jié)

    這篇文章主要介紹了angular 服務(wù)隨記小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Angular工具方法學(xué)習(xí)

    Angular工具方法學(xué)習(xí)

    這篇文章主要為大家詳細(xì)介紹了Angular工具方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • angular實現(xiàn)導(dǎo)航菜單切換

    angular實現(xiàn)導(dǎo)航菜單切換

    這篇文章主要為大家詳細(xì)介紹了angular實現(xiàn)導(dǎo)航菜單切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 詳解基于Angular4+ server render(服務(wù)端渲染)開發(fā)教程

    詳解基于Angular4+ server render(服務(wù)端渲染)開發(fā)教程

    本篇文章主要介紹了詳解基于Angular4+ server render(服務(wù)端渲染)開發(fā)教程 ,具有一定的參考價值,有興趣的可以了解一下
    2017-08-08
  • AngularJS快速入門

    AngularJS快速入門

    本文通過幾個循序漸進(jìn)的例子,給大家詳細(xì)講解了如何快速入門AngularJS,十分的實用,這里推薦給大家,有需要的小伙伴可以參考下。
    2015-04-04
  • 詳解Angular數(shù)據(jù)綁定及其實現(xiàn)方式

    詳解Angular數(shù)據(jù)綁定及其實現(xiàn)方式

    數(shù)據(jù)綁定是將應(yīng)用程序UI或用戶界面綁定到模型的機制。使用數(shù)據(jù)綁定,用戶將能夠使用瀏覽器來操縱網(wǎng)站上存在的元素。
    2021-05-05
  • 如何使用AngularJs打造權(quán)限管理系統(tǒng)【簡易型】

    如何使用AngularJs打造權(quán)限管理系統(tǒng)【簡易型】

    這篇文章主要介紹了使用AngularJs打造權(quán)限管理系統(tǒng)【簡易型】的相關(guān)資料,需要的朋友可以參考下
    2016-05-05
  • 使用angularjs.foreach時return的問題解決

    使用angularjs.foreach時return的問題解決

    這篇文章主要介紹了使用angularjs.foreach時return的問題解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09
  • 使用angular寫一個hello world

    使用angular寫一個hello world

    這篇文章主要介紹了使用angular寫一個hello world的方法及示例,需要的朋友可以參考下
    2015-01-01
  • Angular項目里ngsw-config.json文件作用詳解

    Angular項目里ngsw-config.json文件作用詳解

    這篇文章主要為大家介紹了Angular項目里ngsw-config.json文件作用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11

最新評論