输入验证
设置输入验证,可以在用户输入数据时显示提示信息
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<form ng-app="myApp" ng-controller="myContr" name="myForm">
<p>用户名:<br>
<input type="text" name="username" ng-model="username" required>
<span style="color:yellow" ng-show="myForm.username.$dirty && myForm.username.$invalid">
<span ng-show="myForm.username.$error.required">用户名不能为空</span>
</span>
</p>
<p>邮箱:<br>
<input type="email" name="email" ng-model="email" required>
<span style="color:brown" ng-show="myForm.email.$dirty && myForm.email.$invalid">
<span ng-show="myForm.email.$error.required">邮箱不能为空</span>
<span ng-show="myForm.email.$error.email">邮箱格式不对</span>
</span>
</p>
<p>
<input type="submit" ng-disabled="myForm.username.$dirty && myForm.username.$invalid || myForm.email.$dirty && myForm.email.$invalid">
</p>
</form>
<script>
var app = angular.module('myApp', []);
app.controller('myContr', function ($scope) {
$scope.username = "张敏",
$scope.email = "1210595791@qq.com"
});
</script>
</body>
</html>
html表单属性 novalidate 用于禁用浏览器默认的验证
关于例子里用到的dirty和invalid在前面的博客“AngularJS入门(4)”里说过,忘记的可以去翻一下。