angularjs 表单和控件可以提供验证功能,并对用户输入的非法数据进行警告。
注意:客户端的验证不能确保用户输入数据的安全,所以服务端的数据验证也是必须的。
1、HTML 控件
以下 HTML input 元素被称为 HTML 控件:input 元素、select 元素、button 元素、textarea 元素。
2、HTML 表单
AngularJS 表单是输入控件的集合。HTML 表单通常与 HTML 控件同时存在。
html>
<meta><title>angularJs</title><script></script><div>
<form>
First Name:<br>
<input><br>
Last Name:<br>
<input>
<br><br>
<button>RESET</button>
</form>
<p>form = {{user }}</p>
<p>master = {{master}}</p>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('formCtrl', function($scope) {
$scope.master = {firstName:"John", lastName:"Doe"};
$scope.reset = function() {
$scope.user = angular.copy($scope.master);
};
$scope.reset();
});
</script>
3、输入验证
AngularJS 表单和控件可以提供验证功能,并对用户输入的非法数据进行警告。客户端的验证不能确保用户输入数据的安全,所以服务端的数据验证也是必须的。
html> <meta><title>angularJs</title><script></script><h2>验证实例</h2><script> var app = angular.module('myApp', []); app.controller('validateCtrl', function($scope) { $scope.user = 'John Doe'; $scope.email = 'john.doe@gmail.com'; }); </script>
AngularJS ng-model 指令用于绑定输入元素到模型中。模型对象有两个属性: user 和 email。我们使用了 ng-show指令,color:red 在邮件是 $dirty 或 $invalid 才显示。

没用初始值的输入验证:注意ng-app="",ng-app有值就必须连接到代码模块,利用angular.module 函数来创建模块。
html> <meta><title>angularJs</title><script></script><h2>验证实例</h2>
4、ng-disabled实例
html>
<script></script>
<div>
<form>
Name:
<input><br>
E-mail:
<input><br>
<div>
Invalid:
<span>Tell us your email.</span>
<span>This is not a valid email.</span>
</div>
Gender:<br>
<input>
male
<input>
female<br>
<input>
I agree:
<input>
<div>
Please agree and sign.
</div>
<br>
<!--ng-disabled为true时禁止使用,ng-disabled实时监控应用程序-->
<button>
RESET
</button>
<button>
SAVE
</button>
</form>
</div>
<script>
var app=angular.module("MyApp",[]);
app.controller("Controller",function($scope){
$scope.master = {};
$scope.update=function(user){
$scope.master=$scope.copy(user);
};
$scope.reset=function(){
$scope.user=angular.copy($scope.master);
};
$scope.isUnchanged=function(user){
//判断user和$scope.master是否相等,相等返回true,否则返回false
return angular.equals(user,$scope.master);
};
$scope.reset();
});
</script>
5、ng-submit实例
<meta> <script></script> <!--ng-submit绑定表单提交事件--><script> var app=angular.module("TestFormModule",[]); app.controller("TestFormModule",function($scope){ $scope.user={ userName:"山水子农", password:'' }; $scope.save=function(){ console.log("保存数据中..."); } }); </script>
6、maxlength和minlength实例
html>
<meta>
<script></script>
<title>表单验证</title>
<div>
<form>
<label>Set a maxlength: </label>
<input><br>
<label>Set a minlength: </label>
<input><br><hr>
<label>This input is restricted by the current maxlength and minlength: </label><br>
<input><br>
text input valid? = <code>{{form.text.$valid}}</code><br>
text model = <code>{{textmodel}}</code><br><hr>
<label>This input is restricted by the current maxlength and minlength: </label><br>
<input><br>
number input valid? = <code>{{form.number.$valid}}</code><br>
number model = <code>{{numbermodel}}</code>
</form>
</div>
<script>
var app=angular.module("myApp",[]);
app.controller("myCtrl",function($scope){
$scope.maxlength=8;
$scope.minlength=4;
});
</script>
7、ng-class实例
html>
<meta>
<script></script>
<title>表单验证</title>
<style>
.deleted {
text-decoration: line-through;
}
.bold {
font-weight: bold;
}
.red {
color: red;
}
.error {
color: red;
background-color: yellow;
}
.base-class {
transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 0.5s;
}
.base-class.my-class {
color: red;
font-size:3em;
}
</style>
<div>
<form>
<p>Map Syntax Example</p>
<label><input>deleted (apply "deleted" class)</label><br>
<label><input>bold (apply "bold" class)</label><br>
<label><input>error (apply "error" class)</label>
<hr>
<input>
<input>
<br>
<span>Sample Text</span>
</form>
</div>
<script>
var app=angular.module("myApp",[]);
app.controller("myCtrl",function($scope){
});
</script>
<strong>
</strong>

8、ng-if实例
html>
<meta>
<script></script>
<title>表单验证</title>
<style>
.animate-if {
width:400px;
border:2px solid yellowgreen;
border-radius: 10px;
padding:10px;
display: block;
}
</style>
<div>
<form>
<label>Click me: <input></label><br>
Show when checked:
<span>
This is removed when the checkbox is unchecked.
</span>
</form>
</div>
<script>
var app=angular.module("myApp",[]);
app.controller("myCtrl",function($scope){
});
</script>











