English 中文(简体)
AngularJS - Forms
  • 时间:2024-12-22

AngularJS - Forms


Previous Page Next Page  

AngularJS enriches form filpng and vapdation. We can use ng-cpck event to handle the cpck button and use $dirty and $invapd flags to do the vapdation in a seamless way. Use novapdate with a form declaration to disable any browser-specific vapdation. The form controls make heavy use of AngularJS events. Let us have a look at the events first.

Events

AngularJS provides multiple events associated with the HTML controls. For example, ng-cpck directive is generally associated with a button. AngularJS supports the following events −

    ng-cpck

    ng-dbl-cpck

    ng-mousedown

    ng-mouseup

    ng-mouseenter

    ng-mouseleave

    ng-mousemove

    ng-mouseover

    ng-keydown

    ng-keyup

    ng-keypress

    ng-change

ng-cpck

Reset data of a form using on-cpck directive of a button.

<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-cpck = "reset()">Reset</button>

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

Vapdate Data

The following can be used to track error.

    $dirty − states that value has been changed.

    $invapd − states that value entered is invapd.

    $error − states the exact error.

Example

The following example will showcase all the above-mentioned directives.

testAngularJS.htm

<html>
   <head>
      <title>Angular JS Forms</title>
      <script src = "https://ajax.googleapis.com/ajax/pbs/angularjs/1.3.14/angular.min.js"></script>
      
      <style>
         table, th , td {
            border: 1px sopd 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 Apppcation</h2>
      <span ng-app = "mainApp" ng-controller = "studentController">
         
         <form name = "studentForm" novapdate>
            <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.$invapd">
                        <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.$invapd">
                        <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.$invapd">
                        <span ng-show = "studentForm.email.$error.required">Email is required.</span>
                        <span ng-show = "studentForm.email.$error.email">Invapd email address.</span>
                     </span>
                  </td>
               </tr>
               
               <tr>
                  <td>
                     <button ng-cpck = "reset()">Reset</button>
                  </td>
                  <td>
                     <button ng-disabled = "studentForm.firstname.$dirty &&
                        studentForm.firstname.$invapd || studentForm.lastname.$dirty &&
                        studentForm.lastname.$invapd || studentForm.email.$dirty &&
                        studentForm.email.$invapd" ng-cpck="submit()">Submit</button>
                  </td>
               </tr>
					
            </table>
         </form>
      </span>
      
      <script>
         var mainApp = angular.module("mainApp", []);
         
         mainApp.controller( studentController , function($scope) {
            $scope.reset = function() {
               $scope.firstName = "Mahesh";
               $scope.lastName = "Parashar";
               $scope.email = "MaheshParashar@tutorialspoint.com";
            }
            
            $scope.reset();
         });
      </script>
      
   </body>
</html>

Output

Open the file testAngularJS.htm in a web browser and see the result.