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)

相关文章

  • AngularJS日期格式化常见操作实例分析

    AngularJS日期格式化常见操作实例分析

    这篇文章主要介绍了AngularJS日期格式化常见操作,结合实例形式分析了AngularJS日期格式化常用参数功能、设置与使用技巧,需要的朋友可以参考下
    2018-05-05
  • Angularjs实现多个页面共享数据的方式

    Angularjs实现多个页面共享数据的方式

    本文给大家介绍使用Angularjs实现多个页面共享数据的方式,通过定义一个共享服务service来实现此功能,对angularjs共享数据相关知识感兴趣的朋友一起学习
    2016-03-03
  • 如何以Angular的姿势打开Font-Awesome详解

    如何以Angular的姿势打开Font-Awesome详解

    这篇文章主要给大家介绍了关于如何以Angular的姿势打开Font-Awesome的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Angular具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2018-04-04
  • angular中使用路由和$location切换视图

    angular中使用路由和$location切换视图

    这篇文章主要介绍了angular中使用路由和$location切换视图,需要的朋友可以参考下
    2015-01-01
  • AngularJS 中ui-view传参的实例详解

    AngularJS 中ui-view传参的实例详解

    这篇文章主要介绍了AngularJS 中ui-view传参的实例详解的相关资料,这里提供实例帮助大家学习理解这部分内容,需要的朋友可以参考下
    2017-08-08
  • AngularJS中的$watch(),$digest()和$apply()区分

    AngularJS中的$watch(),$digest()和$apply()区分

    这篇文章主要介绍了AngularJS中的$watch(),$digest()和$apply()区分,感兴趣的朋友可以参考一下
    2016-04-04
  • 详解AngularJS中module模块的导入导出

    详解AngularJS中module模块的导入导出

    本文给大家介绍angularjs中module模块的导入导出,涉及到angularjs module相关知识,对angularjs module感兴趣的朋友一起看看吧
    2015-12-12
  • AngularJS向后端ASP.NET API控制器上传文件

    AngularJS向后端ASP.NET API控制器上传文件

    这篇文章主要介绍了AngularJS向后端ASP.NET API控制器上传文件的相关资料,需要的朋友可以参考下
    2016-02-02
  • angular内容投影详解

    angular内容投影详解

    这篇文章主要为大家介绍了angular内容投影,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2021-12-12
  • AngularJS中实现显示或隐藏动画效果的方式总结

    AngularJS中实现显示或隐藏动画效果的方式总结

    AngularJS 是一组用于创建单页Web应用的丰富框架,给构建丰富交互地应用带来了所有功能,其中一项主要的特性是Angular对动画的支持。下面通过本文给大家介绍AngularJS中实现显示或隐藏动画效果的方式总结,对angularjs动画效果相关知识感兴趣的朋友一起学习
    2015-12-12

最新评论