AngularJS的表单使用详解

 更新时间:2015年06月17日 10:16:30   投稿:goldensun  
这篇文章主要介绍了AngularJS的表单使用详解,在JS原有的基础上提供了更多与HTML交互的功能,需要的朋友可以参考下

 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 - 规定值已被改变。
  •     $invalid- 该值的状态是无效的。
  •     $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。看到结果如下。

2015617100230750.png (715×347)

相关文章

  • angular 服务随记小结

    angular 服务随记小结

    这篇文章主要介绍了angular 服务随记小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05
  • Angular工具方法学习

    Angular工具方法学习

    这篇文章主要为大家详细介绍了Angular工具方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12
  • angular实现导航菜单切换

    angular实现导航菜单切换

    这篇文章主要为大家详细介绍了angular实现导航菜单切换,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • 详解基于Angular4+ server render(服务端渲染)开发教程

    详解基于Angular4+ server render(服务端渲染)开发教程

    本篇文章主要介绍了详解基于Angular4+ server render(服务端渲染)开发教程 ,具有一定的参考价值,有兴趣的可以了解一下
    2017-08-08
  • AngularJS快速入门

    AngularJS快速入门

    本文通过几个循序渐进的例子,给大家详细讲解了如何快速入门AngularJS,十分的实用,这里推荐给大家,有需要的小伙伴可以参考下。
    2015-04-04
  • 详解Angular数据绑定及其实现方式

    详解Angular数据绑定及其实现方式

    数据绑定是将应用程序UI或用户界面绑定到模型的机制。使用数据绑定,用户将能够使用浏览器来操纵网站上存在的元素。
    2021-05-05
  • 如何使用AngularJs打造权限管理系统【简易型】

    如何使用AngularJs打造权限管理系统【简易型】

    这篇文章主要介绍了使用AngularJs打造权限管理系统【简易型】的相关资料,需要的朋友可以参考下
    2016-05-05
  • 使用angularjs.foreach时return的问题解决

    使用angularjs.foreach时return的问题解决

    这篇文章主要介绍了使用angularjs.foreach时return的问题解决,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    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文件作用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-11-11

最新评论