AngularJS的表单使用详解


Posted in Javascript onJune 17, 2015

 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。看到结果如下。

AngularJS的表单使用详解

Javascript 相关文章推荐
基于jquery的web页面日期格式化插件
Nov 15 Javascript
javascript验证身份证号
Mar 03 Javascript
Javascript数据结构与算法之列表详解
Mar 12 Javascript
AngularJS基础 ng-csp 指令详解
Aug 01 Javascript
JavaScript使用readAsDataURL读取图像文件
May 10 Javascript
Vue 父子组件数据传递的四种方式( inheritAttrs + $attrs + $listeners)
May 04 Javascript
a标签调用js的方法总结
Sep 05 Javascript
layui操作列按钮个数和文字颜色的判断实例
Sep 11 Javascript
JS如何实现网站中PC端和手机端自动识别并跳转对应的代码
Jan 08 Javascript
jQuery实现点击滚动到指定元素上的方法分析
Mar 19 jQuery
jQuery+ThinkPHP实现图片上传
Jul 23 jQuery
解决Vue的项目使用Element ui 走马灯无法实现的问题
Aug 03 Javascript
举例讲解AngularJS中的模块
Jun 17 #Javascript
简介AngularJS的HTML DOM支持情况
Jun 17 #Javascript
在Ubuntu系统上安装Ghost博客平台的教程
Jun 17 #Javascript
JavaScript AOP编程实例
Jun 16 #Javascript
js+HTML5基于过滤器从摄像头中捕获视频的方法
Jun 16 #Javascript
动态加载jQuery的方法
Jun 16 #Javascript
详解AngularJS中的表格使用
Jun 16 #Javascript
You might like
php FPDF类库应用实现代码
2009/03/20 PHP
[原创]php简单隔行变色功能实现代码
2016/07/09 PHP
JS target与currentTarget区别说明
2011/08/28 Javascript
jQuery.Validate验证库的使用介绍
2013/04/26 Javascript
node.js中的events.emitter.once方法使用说明
2014/12/10 Javascript
js实现文本框中输入文字页面中div层同步获取文本框内容的方法
2015/03/03 Javascript
浅谈Jquery核心函数
2015/06/18 Javascript
详解Angularjs中的依赖注入
2016/03/11 Javascript
javascript中this关键字详解
2016/12/12 Javascript
jQuery Validate插件自定义验证规则的方法
2016/12/27 Javascript
微信小程序 特效菜单抽屉效果实例代码
2017/01/11 Javascript
BootStrap表单时间选择器详解
2017/05/09 Javascript
你有必要知道的10个JavaScript难点
2017/07/25 Javascript
JS扩展String.prototype.format字符串拼接的功能
2018/03/09 Javascript
JavaScript和TypeScript中的void的具体使用
2019/09/12 Javascript
解决Idea、WebStorm下使用Vue cli脚手架项目无法使用Webpack别名的问题
2019/10/11 Javascript
微信小程序封装多张图片上传api代码实例
2019/12/30 Javascript
[09:13]2014DOTA2国际邀请赛 中国区预选赛coser表演
2014/05/23 DOTA
Python中的条件判断语句基础学习教程
2016/02/07 Python
Python数据类型详解(三)元祖:tuple
2016/05/08 Python
Windows下python2.7.8安装图文教程
2016/05/26 Python
Python中的id()函数指的什么
2017/10/17 Python
python3下实现搜狗AI API的代码示例
2018/04/10 Python
对pycharm代码整体左移和右移缩进快捷键的介绍
2018/07/16 Python
在pandas多重索引multiIndex中选定指定索引的行方法
2018/11/16 Python
Python3实现统计单词表中每个字母出现频率的方法示例
2019/01/28 Python
Python队列、进程间通信、线程案例
2019/10/25 Python
Python函数式编程实例详解
2020/01/17 Python
Django web自定义通用权限控制实现方法
2020/11/24 Python
使用css3绘制出各种几何图形
2016/08/17 HTML / CSS
美国第一香水网站:Perfume.com
2017/01/23 全球购物
Troy-Bilt官网:草坪割草机、吹雪机、分蘖机等
2019/02/19 全球购物
Delphi笔试题
2016/11/14 面试题
2014年幼儿园元旦活动方案
2014/02/13 职场文书
大学运动会入场词
2014/02/22 职场文书
Python批量将csv文件转化成xml文件的实例
2021/05/10 Python