input通常和ngModel结合使用来实现双向绑定,同时angular提供了不少表单校验的指令html
ngTrim 是否trim数据,默认trueui
#html <div ng-controller="LearnCtrl"> <input type="text" ng-model="username" required ng-required="true" ng-minlength="6" ng-maxlength="15" pattern="[0-9]{6,15}" ng-pattern="/^[0-9]{6,15}$/" ng-change="change()" ng-trim="false" /> </div> #script angular.module('learnModule', []) .controller('LearnCtrl', function ($scope) { $scope.change = function () { alert('change'); } });
当input有校验属性时,若是输入的值不符合校验条件,model会被更新成undefined。若是想正常更新model能够经过ngModelOptions设置。url
版本:v1.3.9-local双向绑定
当未设置ngTrueValue和ngFalseValue时,默认值是true和false。code
#html <input type="checkbox" ng-model="check"/> <p>{{check}}</p>
设置了这两个值了,就能够指定选中和未选中的model值。checkbox一样也有ng-chage指令。htm
ngTrueValue和ngFalseValue的参数是表达式哦。ip
#html <div ng-controller="LearnCtrl"> <input type="checkbox" ng-model="check" ng-true-value="'YES'" ng-false-value="'N'+'O'" ng-change="change()"/> <p>{{check}}</p> </div> #script angular.module('learnModule', []) .controller('LearnCtrl', function ($scope) { $scope.check = 'YES'; $scope.change = function () { alert('change'); } });
单选按钮input
没有required属性,没办法作必填校验,因此最好初始化的时候默认选中一个。io
#html <div ng-controller="LearnCtrl"> <input type="radio" ng-model="radio" ng-change="change()" value="value1"/> <input type="radio" ng-model="radio" ng-change="change()" ng-value="'value2'"/> <p>{{radio}}</p> </div> #script angular.module('learnModule', []) .controller('LearnCtrl', function ($scope) { $scope.radio = 'value2'; $scope.change = function () { alert('change'); } });
H5新增的日期选择器。function
若是给date初始化值,model必定得是Date类型,不然会报错。
#html <div ng-controller="LearnCtrl"> <input type="date" ng-model="date" min="2015-12-12" max="2015-12-22" rquired ng-required ng-change="change()"/> <p>{{date}}</p> </div> #script angular.module('learnModule', []) .controller('LearnCtrl', function ($scope) { $scope.date = new Date('2015-12-12'); $scope.change = function () { alert('change'); } });
日期时间选择器
用法基本同input[date],就是比date多了个时间选择。
月份选择器,只能选择年和月。
若是给month初始化值,model必定得是Date类型,不然会报错。
#html <div ng-controller="LearnCtrl"> <input type="month" ng-model="month" required ng-required min="2015-01" max="2015-12" ng-change="change()"/> <p>{{month}}</p> </div> #script angular.module('learnModule', []) .controller('LearnCtrl', function ($scope) { $scope.month = new Date('2015-05'); $scope.change = function () { alert('change'); } });
时间选择
若是给time初始化值,model必定得是Date类型,不然会报错。
#html <div ng-controller="LearnCtrl"> <input type="time" required ng-required min="10:00:00" max="23:00:00" ng-model="time" ng-change="change()"/> <p>{{time}}</p> </div> #script angular.module('learnModule', []) .controller('LearnCtrl', function ($scope) { $scope.time = new Date('2015-12-12 20:00:00'); $scope.change = function () { alert('change'); } });
周选择
若是给week初始化值,model必定得是Date类型,不然会报错。
#html <div ng-controller="LearnCtrl"> <input type="week" ng-model="week" required ng-required min="2015-W12" max="2015-W20" ng-change="change()"/> <p>{{week}}</p> </div> #script angular.module('learnModule', []) .controller('LearnCtrl', function ($scope) { $scope.week = new Date('2015-01-12'); $scope.change = function () { alert('change'); } });
数字类型
即便没有使用校验属性,只要数据不是Number类型,model就会被更新成undefined。
#html <div ng-controller="LearnCtrl"> <input type="number" ng-model="number" required ng-required min="10" max="100" ng-minlength=2 ng-maxlength="3" pattern="3[0-9]{1}" ng-pattern="/^3[0-9]{1}$/" ng-change="change()"/> <p>{{number}}</p> </div> #script angular.module('learnModule', []) .controller('LearnCtrl', function ($scope) { $scope.number = 35; $scope.change = function () { alert('change'); } });
邮箱类型
即便没有使用校验属性,只要数据不符合邮箱格式,model就会被更新成undefined。
#html <div ng-controller="LearnCtrl"> <input type="email" ng-model="email" required ng-required ng-minlength="10" ng-maxlength="20" pattern="1@123.com" ng-pattern="/^1@123.com$/" ng-change="change()"/> <p>{{email}}</p> </div> #script angular.module('learnModule', []) .controller('LearnCtrl', function ($scope) { $scope.email = ''; $scope.change = function () { alert('change'); } });
url类型
即便没有使用校验属性,只要数据不符合url格式,model就会被更新成undefined。
#html <div ng-controller="LearnCtrl"> <input type="url" ng-model="url" required ng-required ng-minlength="6" ng-maxlength="15" pattern="http://www.test.com" ng-pattern="/^http://www.test.com$/" ng-change="change()"/> <p>{{url}}</p> </div> #script angular.module('learnModule', []) .controller('LearnCtrl', function ($scope) { $scope.url = ''; $scope.change = function () { //alert('change'); } });