All for Joomla All for Webmasters

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.       <style>
  7.          table, th , td {
  8.             border: 1px solid grey;
  9.             border-collapse: collapse;
  10.             padding: 5px;
  11.          }
  12.          table tr:nth-child(odd) {
  13.             background-color: lightpink;
  14.          }
  15.          table tr:nth-child(even) {
  16.             background-color: lightyellow;
  17.          }
  18.       </style>
  19.    </head>
  20.    <body>
  21.       <h2>AngularJS Sample Application</h2>
  22.       <div ng-app = “mainApp” ng-controller = “studentController”>
  23.          <form name = “studentForm” novalidate>
  24.             <table border = “0”>
  25.                <tr>
  26.                   <td>Enter first name:</td>
  27.                   <td><input name = “firstname” type = “text” ng-model = “firstName” required>
  28.                      <span style = “color:red” ng-show = “studentForm.firstname.$dirty && studentForm.firstname.$invalid”>
  29.                         <span ng-show = “studentForm.firstname.$error.required”>First Name is required.</span>
  30.                      </span>
  31.                   </td>
  32.                </tr>
  33.                <tr>
  34.                   <td>Enter last name: </td>
  35.                   <td><input name = “lastname”  type = “text” ng-model = “lastName” required>
  36.                      <span style = “color:red” ng-show = “studentForm.lastname.$dirty && studentForm.lastname.$invalid”>
  37.                         <span ng-show = “studentForm.lastname.$error.required”>Last Name is required.</span>
  38.                      </span>
  39.                   </td>
  40.                </tr>
  41.                <tr>
  42.                   <td>Email: </td><td><input name = “email” type = “email” ng-model = “email” length = “100” required>
  43.                      <span style = “color:red” ng-show = “studentForm.email.$dirty && studentForm.email.$invalid”>
  44.                         <span ng-show = “studentForm.email.$error.required”>Email is required.</span>
  45.                         <span ng-show = “studentForm.email.$error.email”>Invalid email address.</span>
  46.                      </span>
  47.                   </td>
  48.                </tr>
  49.                <tr>
  50.                   <td>
  51.                      <button ng-click = “reset()”>Reset</button>
  52.                   </td>
  53.                   <td>
  54.                      <button ng-disabled = “studentForm.firstname.$dirty &&
  55.                         studentForm.firstname.$invalid || studentForm.lastname.$dirty &&
  56.                         studentForm.lastname.$invalid || studentForm.email.$dirty &&
  57.                         studentForm.email.$invalid” ng-click=“submit()”>Submit</button>
  58.                   </td>
  59.                </tr>
  60.             </table>
  61.          </form>
  62.       </div>
  63.       <script>
  64.          var mainApp = angular.module(“mainApp”, []);
  65.          mainApp.controller(‘studentController’, function($scope) {
  66.             $scope.reset = function(){
  67.                $scope.firstName = “make”;
  68.                $scope.lastName = “sure”;
  69.                $scope.email = “info@makeforsure.com”;
  70.             }
  71.             $scope.reset();
  72.          });
  73.       </script>
  74.    </body>
  75. </html>
PinIt
submit to reddit

Leave a Reply

Top