AngularJS 表单


AngularJS 丰富了表单填充和验证。我们可以使用 ng-click 事件来处理点击按钮,并使用 $dirty 和 $invalid 标志以无缝方式进行验证。使用 novalidate 和表单声明来禁用任何特定于浏览器的验证。表单控件大量使用 AngularJS 事件。让我们先来看看事件。

Events


AngularJS 提供了与 HTML 控件相关的多个事件。例如,ng-click 指令通常与按钮相关联。 AngularJS 支持以下事件:

  • ng-click
  • ng-dbl-点击
  • ng-mousedown
  • ng-mouseup
  • ng-mouseenter
  • ng-mouseleave
  • ng-鼠标移动
  • ng-鼠标悬停
  • ng-keydown
  • ng-keyup
  • ng-keypress
  • ng-改变

ng-click


使用按钮的单击指令重置表单的数据。

<input name = "firstname" type = "text" ng-model = "firstName" required>
<input name = "lastname" type = "text" ng-model = "lastName" required>
<input name = "email" type = "email" ng-model = "email" required>
<button ng-click = "reset()">Reset</button>

<script>
    function studentController($scope) {
        $scope.reset = function() {
            $scope.firstName = "Mahesh";
            $scope.lastName = "Parashar";
            $scope.email = "MaheshParashar@newbiego.com";
        }
      
        $scope.reset();
    }
</script>

验证数据


以下可用于跟踪错误。

  • $dirty : 表示值已更改。

  • $invalid :表示输入的值无效。

  • $error : 说明准确的错误。

例子


以下示例将展示所有上述指令。

测试AngularJS.htm

<html>
    <head>
        <title>Angular JS Forms</title>
        <script src = "https:// ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">
      
        <style>
            table, th , td {
                border: 1px solid grey;
                border-collapse: collapse;
                padding: 5px;
            }
            table tr:nth-child(odd) {
                background-color: #f2f2f2;
            }
            table tr:nth-child(even) {
                background-color: #ffffff;
            }
        </style>
      
    </head>
    <body>
      
        <h2>AngularJS Sample Application</h2>
        <div ng-app = "mainApp" ng-controller = "studentController">
         
            <form name = "studentForm" novalidate>
                <table border = "0">
                    <tr>
                        <td>Enter first name:</td>
                        <td><input name = "firstname" type = "text" ng-model = "firstName" required>
                            <span style = "color:red" ng-show = "studentForm.firstname.$dirty && studentForm.firstname.$invalid">
                                <span ng-show = "studentForm.firstname.$error.required">First Name is required.</span>
                            </span>
                        </td>
                    </tr>
               
                    <tr>
                        <td>Enter last name: </td>
                        <td><input name = "lastname"  type = "text" ng-model = "lastName" required>
                            <span style = "color:red" ng-show = "studentForm.lastname.$dirty && studentForm.lastname.$invalid">
                                <span ng-show = "studentForm.lastname.$error.required">Last Name is required.</span>
                            </span>
                        </td>
                    </tr>
               
                    <tr>
                        <td>Email: </td><td><input name = "email" type = "email" ng-model = "email" length = "100" required>
                            <span style = "color:red" ng-show = "studentForm.email.$dirty && studentForm.email.$invalid">
                                <span ng-show = "studentForm.email.$error.required">Email is required.</span>
                                <span ng-show = "studentForm.email.$error.email">Invalid email address.</span>
                            </span>
                        </td>
                    </tr>
               
                    <tr>
                        <td>
                            <button ng-click = "reset()">Reset</button>
                        </td>
                        <td>
                            <button ng-disabled = "studentForm.firstname.$dirty &&
                                studentForm.firstname.$invalid || studentForm.lastname.$dirty &&
                                studentForm.lastname.$invalid || studentForm.email.$dirty &&
                                studentForm.email.$invalid" ng-click="submit()">Submit</button>
                        </td>
                    </tr>
					
                </table>
            </form>
        </div>
      
        <script>
            var mainApp = angular.module("mainApp", []);
         
            mainApp.controller('studentController', function($scope) {
                $scope.reset = function() {
                    $scope.firstName = "Mahesh";
                    $scope.lastName = "Parashar";
                    $scope.email = "MaheshParashar@newbiego.com";
                }
            
                $scope.reset();
            });
        </script>
      
    </body>
</html>

输出


打开文件 测试AngularJS.htm 在网络浏览器中查看结果。