23/01/2018
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:
First Name:
var app = angular.module('myApp', []);
app.controller('formCtrl', function($scope) {
$scope.firstname = "Ajeet";
});
You can also change the example in the following way:
See this example:
First Name:
You entered: {{firstname}}
Change the name inside the input field, and you will see the name in the header changes accordingly.
AngularJS Checkbox
A checkbox has a value true or false. The ng-model directive is used for a checkbox.
See this example:
Check to show this:
Checked
The ng-show attribute is set to true when the checkbox is checked.
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:
Pick a topic:
Tutorials
Festivals
News
Tutorials
Welcome to the best tutorials over the net
Festivals
Most famous festivals
News
Welcome to the news portal.
The ng-switch directive hides and shows HTML sections depending on the value of the radio buttons.
AngularJS Selectbox
ng-model directive is used to bind select boxes to your application.
See this example:
Select a topic:
Tutorials
Festivals
News
Tutorials
Welcome to the best tutorials over the net.
Festivals
Most famous festivals.
News
Welcome to the news portal.
The ng-switch directive hides and shows HTML sections depending on the value of the radio buttons.
AngularJS form example
Angular JS Forms
table, th , td {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
}
table tr:nth-child(odd) {
background-color: lightpink;
}
table tr:nth-child(even) {
background-color: lightyellow;
}
AngularJS Sample Application
Enter first name:
First Name is required.
Enter last name:
Last Name is required.
Email:
Email is required.
Invalid email address.
Reset
Submit
var mainApp = angular.module("mainApp", []);
mainApp.controller('studentController', function($scope) {
$scope.reset = function(){
$scope.firstName = "Nitin";
$scope.lastName = "Goyal";
$scope.email = "[email protected]";
}
$scope.reset();
});
/* AngularJS v1.4.8 (c) 2010-2015 Google, Inc. http://angularjs.org License: MIT */ (function(S,X,u){'use strict';function G(a){return function(){var b=arguments[0],d;d="["+(a?a+":":"")+b+"] http://errors.angularjs.org/1.4.8/"+(a?a+"/":"")+b;for(b=1;b").append(a).html())):c?Pa.clone.call(a):a;if(k)f...