AngularJS - แบบฟอร์ม

AngularJS เสริมสร้างการกรอกแบบฟอร์มและการตรวจสอบความถูกต้อง เราสามารถใช้เหตุการณ์ ng-click เพื่อจัดการกับปุ่มคลิกและใช้แฟล็ก $ dirty และ $ invalid เพื่อทำการตรวจสอบความถูกต้องได้อย่างราบรื่น ใช้ novalidate กับการประกาศแบบฟอร์มเพื่อปิดใช้งานการตรวจสอบความถูกต้องเฉพาะเบราว์เซอร์ ตัวควบคุมแบบฟอร์มใช้ประโยชน์จากเหตุการณ์ AngularJS อย่างหนัก ให้เราดูเหตุการณ์ก่อน

เหตุการณ์

AngularJS มีหลายเหตุการณ์ที่เกี่ยวข้องกับการควบคุม HTML ตัวอย่างเช่นโดยทั่วไปคำสั่ง ng-click จะเชื่อมโยงกับปุ่ม AngularJS รองรับเหตุการณ์ต่อไปนี้ -

  • ng-click
  • ng-dbl-click
  • ng-mousedown
  • ng-mouseup
  • ng-mouseenter
  • ng-mouseleave
  • ng-mousemove
  • ng-mouseover
  • ng-keydown
  • ng-keyup
  • ng-keypress
  • ng-change

ng- คลิก

รีเซ็ตข้อมูลของแบบฟอร์มโดยใช้คำสั่งเมื่อคลิกปุ่ม

<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 = "[email protected]";
      }   
      
      $scope.reset();
   }
</script>

ตรวจสอบข้อมูล

สิ่งต่อไปนี้สามารถใช้เพื่อติดตามข้อผิดพลาด

  • $dirty - ระบุว่ามีการเปลี่ยนแปลงค่า

  • $invalid - ระบุว่าค่าที่ป้อนไม่ถูกต้อง

  • $error - ระบุข้อผิดพลาดที่แน่นอน

ตัวอย่าง

ตัวอย่างต่อไปนี้จะแสดงคำสั่งที่กล่าวถึงข้างต้นทั้งหมด

testAngularJS.htm

<html>
   <head>
      <title>Angular JS Forms</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
      
      <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 = "[email protected]";
            }
            
            $scope.reset();
         });
      </script>
      
   </body>
</html>

เอาต์พุต

เปิดไฟล์testAngularJS.htmในเว็บเบราว์เซอร์และดูผลลัพธ์