ng
对input元素的type进行了扩展,一共提供了以下10中类型:text, number, url , email, radio, checkbox, hidden, button, submit, reset
对表单内置4种CSS样式:ng-valid, ng-invalid, ng-pristine, ng-dirty
对表单内置校验器:require, minlength, maxlenghth
案例代码
ng-pristine 表单未填写时的CSS样式
ng-valid 表单验证有效时的CSS样式
ng-invalid 表单验证无效时的CSS样式
required 指定表单控件必须填写
ng-maxlength="6" 指定表单控件填写内容的最大长度6---用户名
ng-minlength="6" 指定表单控件填写内容的最小长度6---密码
ng-model="user.password" 指定表单控件双向绑定的变量
ng-submit="save()" 指定该表单提交事件的处理函数save()
ng-disabled="myForm.$invalid" 检验提交按钮的是否可用根据表单的验证状态
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script>
<style>
.my-form {transition:all linear 0.5s;background: transparent;}
/* 定义表单未填写时的CSS样式 */
.my-form.ng-pristine {background: lightGray;}
/* 定义表单验证无效时的CSS样式 */
.my-form.ng-invalid {background: lightGray;}
/* 定义表单验证有效的CSS样式 */
.my-form.ng-valid{background: green;}
</style>
</head>
<body ng-app="mshApp">
<form name="myForm" class="my-form" ng-submit="save()" ng-controller="formCrtl" >
用户:<input name="userName" type="text" ng-model="user.userName" ng-maxlength="9" required /><p>
密码:<input name="password" type="password" ng-model="user.password" ng-minlength="6" required/><p>
<input type="submit" ng-disabled="myForm.$invalid"/>
</form>
<script>
var app = angular.module('mshApp',[]);
app.controller("formCrtl",setUser);
function setUser($scope){
$scope.user = {
userName :'',
password :''
};
$scope.save = function(){
alert("保存数据!"+$scope.user.userName+"-"+$scope.user.password);
}
};
</script>
</body>
</html>