运行 ❯
获取您的
自己的
网站
×
更改方向
保存代码
更改主题,暗/亮
转到 Spaces
<!DOCTYPE html> <html> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script> <body ng-app="myApp"> <p>Try writing in the input field:</p> <form name="myForm"> <input name="myInput" ng-model="myInput" required my-directive> </form> <p>The input's valid state is:</p> <h1>{{myForm.myInput.$valid}}</h1> <script> var app = angular.module('myApp', []); app.directive('myDirective', function() { return { require: 'ngModel', link: function(scope, element, attr, mCtrl) { function myValidation(value) { if (value.indexOf("e") > -1) { mCtrl.$setValidity('charE', true); } else { mCtrl.$setValidity('charE', false); } return value; } mCtrl.$parsers.push(myValidation); } }; }); </script> <p>The input field must contain the character "e" to be consider valid.</p> </body> </html>