欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

AngularJS实现的获取焦点及失去焦点时的表单验证功能示例

程序员文章站 2022-06-25 13:34:31
本文实例讲述了angularjs实现的获取焦点及失去焦点时的表单验证功能。分享给大家供大家参考,具体如下: <...

本文实例讲述了angularjs实现的获取焦点及失去焦点时的表单验证功能。分享给大家供大家参考,具体如下:

<!doctype html>
<html ng-app="formexample">
<head>
  <meta charset="utf-8">
  <title></title>
  <script src="../js/angular.js"></script>
  <script>
    angular.module('formexample', [])
        .controller('formcontroller', ['$scope', function($scope)
        {
          $scope.usertype = 'guest';
          $scope.change = false;
        }]);
  </script>
</head>
<body>
<form name="myform" ng-controller="formcontroller">
  usertype: <input name="input" ng-model="usertype" ng-blur="change=true" ng-focus="change=false" required>
  <span class="error" ng-show="myform.input.$error.required && change">必填项</span><br>
</form>
</body>
</html>

运行效果:

AngularJS实现的获取焦点及失去焦点时的表单验证功能示例

更多关于angularjs相关内容感兴趣的读者可查看本站专题:《angularjs指令操作技巧总结》、《angularjs入门与进阶教程》及《angularjs mvc架构总结

希望本文所述对大家angularjs程序设计有所帮助。