AngularJS Forms

AngularJS Forms with examples on mvc, expression, directive, controller, module, scope, filter, dom, form, ajax, validation, services, animation, dependency injection and more.

 0
AngularJS Forms

AngularJS Forms

AngularJS facilitates you to create a form enriches with data binding and validation of input controls.

Input controls are ways for a user to enter data. A form is a collection of controls for the purpose of grouping related controls together.

Following are the input controls used in AngularJS forms:

  • input elements
  • select elements
  • button elements
  • textarea elements

AngularJS provides multiple events that can be associated with the HTML controls. These events are associated with the different HTML input elements.

Following is a list of events supported in 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

Data Binding

ng-model directive is used to provide data binding.

Let's take an example where ng-model directive binds the input controller to the rest of your application

See this example:

  1.    <!DOCTYPE html>  
  2. <html lang="en">  
  3. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
  4. <body>  
  5. <div ng-app="myApp" ng-controller="formCtrl">  
  6.   <form>  
  7.     First Name: <input type="text" ng-model="firstname">  
  8.   </form>  
  9. </div>  
  10. <script>  
  11. var app = angular.module('myApp', []);  
  12. app.controller('formCtrl', function($scope) {  
  13.     $scope.firstname = "Ajeet";  
  14. });  
  15. </script>  
  16. </body>  
  17. </html>  

You can also change the example in the following way:

See this example:

  1. <!DOCTYPE html>  
  2. <html>  
  3. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
  4. <body>  
  5. <div ng-app="">  
  6.   <form>  
  7.     First Name: <input type="text" ng-model="firstname">  
  8.   </form>  
  9.   <h2>You entered: {{firstname}}</h2>  
  10. </div>  
  11. <p>Change the name inside the input field, and you will see the name in the header changes accordingly.</p>  
  12. </body>  
  13. </html> 

AngularJS Checkbox

A checkbox has a value true or false. The ng-model directive is used for a checkbox.

See this example:

  1. <!DOCTYPE html>  
  2. <html>  
  3. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
  4. <body>  
  5. <div ng-app="">  
  6.   <form>  
  7.     Check to show this:  
  8.     <input type="checkbox" ng-model="myVar">  
  9.   </form>  
  10.   <h1 ng-show="myVar">Checked</h1>  
  11. </div>  
  12. <p>The ng-show attribute is set to true when the checkbox is checked.</p>  
  13. </body>  
  14. </html> 

AngularJS Radio Buttons

ng-model directive is used to bind radio buttons in your applications.

Let's take an example to display some text, based on the value of the selected radio buttons. In this example, we are also using ng-switch directive to hide and show HTML sections depending on the value of the radio buttons.

See this example:

  1. <!DOCTYPE html>  
  2. <html>  
  3. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
  4. <body ng-app="">  
  5. <form>  
  6.   Pick a topic:  
  7.   <input type="radio" ng-model="myVar" value="tuts">Tutorials  
  8.   <input type="radio" ng-model="myVar" value="fest">Festivals  
  9.   <input type="radio" ng-model="myVar" value="news">News  
  10. </form>  
  11. <div ng-switch="myVar">  
  12.   <div ng-switch-when="tuts">  
  13.      <h1>Tutorials</h1>  
  14.      <p>Welcome to the best tutorials over the net</p>  
  15.   </div>  
  16.   <div ng-switch-when="fest">  
  17.      <h1>Festivals</h1>  
  18.      <p>Most famous festivals</p>  
  19.   </div>  
  20.   <div ng-switch-when="news">  
  21.      <h1>News</h1>  
  22.      <p>Welcome to the news portal.</p>  
  23.   </div>  
  24. </div>  
  25. <p>The ng-switch directive hides and shows HTML sections depending on the value of the radio buttons.</p>  
  26. </body>  
  27. </html> 

AngularJS Selectbox

ng-model directive is used to bind select boxes to your application.

See this example:

  1. <!DOCTYPE html>  
  2. <html>  
  3. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
  4. <body ng-app="">  
  5. <form>  
  6.   Select a topic:  
  7.   <select ng-model="myVar">  
  8.     <option value="">  
  9.     <option value="tuts">Tutorials  
  10.     <option value="fest">Festivals  
  11.     <option value="news">News  
  12.   </select>  
  13. </form>  
  14. <div ng-switch="myVar">  
  15.   <div ng-switch-when="tuts">  
  16.      <h1>Tutorials</h1>  
  17.      <p>Welcome to the best tutorials over the net.</p>  
  18.   </div>  
  19.   <div ng-switch-when="fest">  
  20.      <h1>Festivals</h1>  
  21.      <p>Most famous festivals.</p>  
  22.   </div>  
  23.   <div ng-switch-when="news">  
  24.      <h1>News</h1>  
  25.      <p>Welcome to the news portal.</p>  
  26.   </div>  
  27. </div>  
  28. <p>The ng-switch directive hides and shows HTML sections depending on the value of the radio buttons.</p>  
  29. </body>  
  30. </html> 

AngularJS form example

  1. <!DOCTYPE html>  
  2. <html>  
  3.    <head>  
  4.       <title>Angular JS Forms</title>  
  5.       <script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>  
  6.         
  7.       <style>  
  8.          table, th , td {  
  9.             border: 1px solid grey;  
  10.             border-collapse: collapse;  
  11.             padding: 5px;  
  12.          }  
  13.            
  14.          table tr:nth-child(odd) {  
  15.             background-color: lightpink;  
  16.          }  
  17.            
  18.          table tr:nth-child(even) {  
  19.             background-color: lightyellow;  
  20.          }  
  21.       </style>  
  22.         
  23.    </head>  
  24.    <body>  
  25.         
  26.       <h2>AngularJS Sample Application</h2>  
  27.       <div ng-app = "mainApp" ng-controller = "studentController">  
  28.            
  29.          <form name = "studentForm" novalidate>  
  30.             <table border = "0">  
  31.                <tr>  
  32.                   <td>Enter first name:</td>  
  33.                   <td><input name = "firstname" type = "text" ng-model = "firstName" required>  
  34.                      <span style = "color:red" ng-show = "studentForm.firstname.$dirty && studentForm.firstname.$invalid">  
  35.                         <span ng-show = "studentForm.firstname.$error.required">First Name is required.</span>  
  36.                      </span>  
  37.                   </td>  
  38.                </tr>  
  39.                  
  40.                <tr>  
  41.                   <td>Enter last name: </td>  
  42.                   <td><input name = "lastname"  type = "text" ng-model = "lastName" required>  
  43.                      <span style = "color:red" ng-show = "studentForm.lastname.$dirty && studentForm.lastname.$invalid">  
  44.                         <span ng-show = "studentForm.lastname.$error.required">Last Name is required.</span>  
  45.                      </span>  
  46.                   </td>  
  47.                </tr>  
  48.                  
  49.                <tr>  
  50.                   <td>Email: </td><td><input name = "email" type = "email" ng-model = "email" length = "100" required>  
  51.                      <span style = "color:red" ng-show = "studentForm.email.$dirty && studentForm.email.$invalid">  
  52.                         <span ng-show = "studentForm.email.$error.required">Email is required.</span>  
  53.                         <span ng-show = "studentForm.email.$error.email">Invalid email address.</span>  
  54.                      </span>  
  55.                   </td>  
  56.                </tr>  
  57.                  
  58.                <tr>  
  59.                   <td>  
  60.                      <button ng-click = "reset()">Reset</button>  
  61.                   </td>  
  62.                   <td>  
  63.                      <button ng-disabled = "studentForm.firstname.$dirty &&  
  64.                         studentForm.firstname.$invalid || studentForm.lastname.$dirty &&  
  65.                         studentForm.lastname.$invalid || studentForm.email.$dirty &&  
  66.                         studentForm.email.$invalid" ng-click="submit()">Submit</button>  
  67.                   </td>  
  68.                </tr>  
  69.                       
  70.             </table>  
  71.          </form>  
  72.       </div>  
  73.         
  74.       <script>  
  75.          var mainApp = angular.module("mainApp", []);  
  76.            
  77.          mainApp.controller('studentController', function($scope) {  
  78.             $scope.reset = function(){  
  79.                $scope.firstName = "Sonoo";  
  80.                $scope.lastName = "Jaiswal";  
  81.                $scope.email = "sonoojaiswal@javatpoint.com";  
  82.             }  
  83.               
  84.             $scope.reset();  
  85.          });  
  86.       </script>  
  87.         
  88.    </body>  
  89. </html>