<HTMLNg-app= "MYAPP"><Head> <MetaCharSet= "UTF-8"> <title>Test form Validation</title> <Scripttype= "Text/javascript"src= "Lib/angular/angular.js"></Script> <Scripttype= "Text/javascript"> varapp=Angular.module ('myApp', []); App.controller ('Testctrl', function($scope) {var_test= function() {$scope. Cansubmit= false; Console.log ('a'); }; varInit= function() {$scope. Model={name:'happen' }; $scope. Cansubmit= true; $scope. Test=_test; }; Init (); }); App.directive ('Testvalid', function() { return{restrict:'A', require:'Ngmodel', Link:function(Scope, Elem, Attrs, ctrl) {varInit= function() {Elem.on ('Blur', function() {scope. $apply (function() { if(Elem.val ()=== 'happen') {Ctrl. $setValidity ('Ishappen', false); } Else{Ctrl. $setValidity ('Ishappen', true); } }); }); }; Init (); } } }); </Script></Head><Body> <formname= "MyForm"> <DivNg-controller= "Testctrl"> <inputtype= "text"name= "Test"Ng-model= "Model.name"Test-valid Required> <PNg-show= "myform.test. $error. Ishappen">Name is the default value</P> <PNg-show= "myform.test. $error. Required">Name must be filled in</P> <Buttonng-disabled= "MyForm. $invalid | |!cansubmit"Ng-click= "Test ();"style= "width:200px;height:20px;"></Button> </Div> </form></Body></HTML>
Simple angular form validation directives