【AngularJS: Up & Running】第04章_表单、输入和服务
来源:互联网 发布:富士康跳樓事件知乎 编辑:程序博客网 时间:2024/06/08 09:55
1 ng-model的使用
ng-model指令:进行数据的双向绑定
<input type="text" ng-model="ctrl.username">You typed {{ctrl.username}}
angular.module('notesApp', []) .controller('MainCtrl', [function () { this.username = 'nothing';}]);
2 表单的使用
<form ng-submit="ctrl.submit();"> <input type="text" ng-model="ctrl.user.username"> <input type="password" ng-model="ctrl.user.password"> <input type="submit" value="submit"></form>
var self = this;self.submit = function () { console.log('User clicked submit with ', self.user);};
3 使用数据绑定和模型
HTML:
<form ng-submit="ctrl.submit1();"> <input type="text" ng-model="ctrl.username"> <input type="password" ng-model="ctrl.password"> <input type="submit" value="submit"></form><form ng-submit="ctrl.submit2();"> <input type="text" ng-model="ctrl.user.username"> <input type="password" ng-model="ctrl.user.password"> <input type="submit" value="submit"></form>
JS:
var self = this;self.submit1 = function () { //创建了一个代表用户的对象并发送给服务器 var user = {username:self.username,password:self.password}; console.log('First form submit with ',user);};self.submit2 = function () { console.log('Second form submit with ',self.user);};
4 表单状态及验证
HTML
<form ng-submit="ctrl.submit();" name="myForm"> <input type="text" ng-model="ctrl.user.username" required ng-minlength="4"> <input type="password" ng-model="ctrl.user.password" required> <input type="submit" value="Submit" ng-disabled="myForm.$invalid"><!-- 通过表单名访问表单状态 --></form>
JS
angular.module('notesApp', []) .controller('MainCtrl', [function () { var self = this; self.submit = function () { console.log('User clicked submit with ', self.user); }; }]);
4.1 AngularJS中表单的状态属性
4.2 AngularJS内置的验证器
出了上述验证器之外,用户也可以编写自定义验证器。
5 显示错误信息
ng-show的使用
<form ng-submit="ctrl.submit();" name="myForm"> <input type="text" name="uname" ng-model="ctrl.user.username" required ng-minlength="4"> <span ng-show="myForm.uname.$error.required"> This is a required field </span> <span ng-show="myForm.uname.$error.minlength"> Minimum length required is 4 </span> <span ng-show="myForm.uname.$invalid"> This field is invalid </span> <input type="password" name="pwd" ng-model="ctrl.user.password" required> <span ng-show="myForm.pwd.$error.required"> This is a required field </span> <input type="submit" value="Submit" ng-disabled="myForm.$invalid"></form>
6 根据状态修改表单样式
6.1 表单状态CSS class
6.2 输入控件状态CSS class
6.3 不同方式高亮显示输入控件
HTML:
<form ng-submit="ctrl.submit();" name="myForm"> <input type="text" class="username" name="uname" ng-model="ctrl.user.username" required ng-minlength="4"> <input type="submit" value="Submit" ng-disabled="myForm.$invalid"></form>
CSS:
<style> .username.ng-valid{ background-color: green; } .username.ng-dirty.ng-invalid-required{ background-color: red; } .username.ng-dirty.ng-invalid-minlength{ background-color: lightpink; } </style>
JS:
angular.module('notesApp', []) .controller('MainCtrl', [function () { var self = this; self.submit = function () { console.log('User clicked submit with ', self.user); }; }]);
7 ng-form与内嵌表单
AngularJS提供了ng-form指令,它与form非常相近,同时又提供了内嵌功能,这样一来,我们就能将表单中的一些字段组合起来,当作一个自快进行处理。
<form novalidate name="myForm"> <input type="text" class="username" name="uname" ng-model="ctrl.user.username" required="" placeholder="Username" ng-minlength="4" /> <input type="password" class="password" name="pwd" ng-model="ctrl.user.password" placeholder="Password" required="" /> <ng-form name="profile"> <input type="text" name="firstName" ng-model="ctrl.user.profile.firstName" placeholder="First Name" required> <input type="text" name="middleName" placeholder="Middle Name" ng-model="ctrl.user.profile.middleName"> <input type="text" name="lastName" placeholder="Last Name" ng-model="ctrl.user.profile.lastName" required> <input type="date" name="dob" placeholder="Date Of Birth" ng-model="ctrl.user.profile.dob" required> </ng-form> <span ng-show="myForm.profile.$invalid"> Please fill out the profile information </span> <input type="submit" value="Submit" ng-disabled="myForm.$invalid"></form>
- 1 通过ng-form指令创建出子菜单,用户可以给该表单分配名称,用于获取它本身和它的状态
- 2 子表单的状态的既能直接通过 (子表单名.
valid)访问,也能通过它的父表单访问(父表单名.子表单名. invalid) - 3 表单中的每一个元素都可以通过普通方式(表单名.firstName.$error.required)访问
- 4 子表单和内嵌表单会影响外部表单(myForm.$invalid是true,因为使用了required)
8 其他表单控件
<!--多行文本输入框(Textarea)--> <textarea ng-model="ctrl.user.address" required></textarea> <!--复选框(Checkbox)--> <input type="checkbox" ng-model="ctrl.user.agree" ng-true-value="YES" ng-false-value="NO"> <!--单选框(RadioButton)--> <div ng-init="user = {gender:'female'}"> <input type="radio" name="gender" ng-model="user.gender" value="male"> <input type="radio" name="gender" ng-model="user.gender" value="female"> </div> <!--下拉框(Combo Boxes/Drop-Downs)--> <div ng-init="location = 'India'"> <select ng-model="location"> <option value="USA">USA</option> <option value="India">India</option> <option value="Other">None of the above</option> </select> </div>
0 0
- 【AngularJS: Up & Running】第04章_表单、输入和服务
- 【AngularJS: Up & Running】第05章_AngularJS服务揭秘
- 【AngularJS: Up & Running】第02章_基本的AngularJS指令及控制器
- AngularJS表单和输入验证
- AngularJS 表单和输入验证
- JavaScript权威指南_139_第15章_脚本化文档_15.9-HTML表单-选取表单和表单元素
- HTML5表单_输入类型
- JavaScript权威指南_140_第15章_脚本化文档_15.9-HTML表单-表单和元素的属性
- AngularJS服务和自定义服务
- 第 4 章 祝福吧!把表单和输入控件都改成ext的样式
- 第 4 章 祝福吧!把表单和输入控件都改成ext的样式
- 第04章_函数和递归
- Android学习第四周_服务、广播和酷特性
- Swift Up and Running——变量和常量
- Swift Up and Running——整数和浮点数
- Swift Up and Running——字符、Unicode和字符串
- HTML 表单和输入
- HTML 表单和输入
- 新篇章
- Android Studio 2.2 正式起航
- Java面试基础部分(1)
- 【50.54%】【BZOJ 1879】[Sdoi2009]Bill的挑战
- Python--常用语句
- 【AngularJS: Up & Running】第04章_表单、输入和服务
- iOS – Quartz2D & CALayer & CAShapeLayer
- jsp内置对象之session
- 协同过滤itembase计算Spark实现(三)
- your cpu does not support NX
- Android Studio 2.2 正式起航
- bzoj1651(差分)
- (git)Bitbucket使用WebHook实现自动部署PHP代码
- mybaties日常开发总结