The code is very simple, so I don’t talk much nonsense, just present the code:
The code copy is as follows:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Unt titled document</title>
</head>
<script src="http://localhost:81/js/jquery.js">
</script>
<script src="http://localhost:81/js/angular.min.js">
</script>
<body ng-app="app">
<div ng-controller="TestCtrl">
<div ng-form test>
<input ng-model="a" type="email" />
<button ng-click="do()">View</button>
</div>
</div>
<script>
app = angular.module("app",[]);
app.directive('test',function() {<br> //The commands of the form form have a default controller as the fourth parameter
var link = function($scope, $element, $attrs, $ctrl) {
$scope.do = function() {
//$ctrl.$setDirty();
console.log($ctrl.$pristine); //Is form not passive?
console.log($ctrl.$dirty); // Is form passive?
console.log($ctrl.$valid); // Whether form has been checked
console.log($ctrl.$invalid); //Is there any error in form
console.log($ctrl.$error); // There is an error field in form
}
}
return {
compile: function() {
return link
},
require: 'form',
restrict: 'A'
}
});
app.controller('TestCtrl', function($scope){
//If there is no controller, this thing will not be initialized yet..
});
</script>
</body>
</html>
What I want to share with you is the most basic angularJS form verification, I hope you like it.