AngularJS Forms

Toggle navigation
TUTORIAL HOME
AngularJS Forms
❮ Previous Next ❯
Forms in AngularJS provides data-binding and validation of input controls.

Input Controls
Input controls are the HTML input elements:

input elements
select elements
button elements
textarea elements
Data-Binding
Input controls provides data-binding by using the ng-model directive.

The application does now have a property named firstname.

The ng-model directive binds the input controller to the rest of your application.

The property firstname, can be referred to in a controller:

Example

var app = angular.module(‘myApp’, []);
app.controller(‘formCtrl’, function($scope) {
    $scope.firstname = “John”;
});

»
It can also be referred to elsewhere in the application:

Example

First Name:

You entered: {{firstname}}

»
Checkbox
A checkbox has the value true or false. Apply the ng-model directive to a checkbox, and use its value in your application.

Example
Show the header if the checkbox is checked:

    Check to show a header:
   

My Header

»
Radiobuttons
Bind radio buttons to your application with the ng-model directive.

Radio buttons with the same ng-model can have different values, but only the selected one will be used.

Example
Display some text, based on the value of the selected radio button:

Pick a topic:
Dogs
Tutorials
Cars

»
The value of myVar will be either dogs, tuts, or cars.

Selectbox
Bind select boxes to your application with the ng-model directive.

The property defined in the ng-model attribute will have the value of the selected option in the selectbox.

Example
Display some text, based on the value of the selected option:

Select a topic:

   
    Dogs
    Tutorials
    Cars

»
The value of myVar will be either dogs, tuts, or cars.

An AngularJS Form Example
First Name:

Last Name:

RESET
form = {{user}}

master = {{master}}

Application Code

 
    First Name:

   

    Last Name:

   
   

    RESET</button>
 
 

form = {{{user}}

 

master = {{{master}}

var app = angular.module(‘myApp’, []);
app.controller(‘formCtrl’, function($scope) {
    $scope.master = {firstName: “John”, lastName: “Doe”};
    $scope.reset = function() {
        $scope.user = angular.copy($scope.master);
    };
    $scope.reset();
});

»
The novalidate attribute is new in HTML5. It disables any default browser validation.

Example Explained
The ng-app directive defines the AngularJS application.

The ng-controller directive defines the application controller.

The ng-model directive binds two input elements to the user object in the model.

The formCtrl controller sets initial values to the master object, and defines the reset() method.

The reset() method sets the user object equal to the master object.

The ng-click directive invokes the reset() method, only if the button is clicked.

The novalidate attribute is not needed for this application, but normally you will use it in AngularJS forms, to override standard HTML5 validation.

❮ Previous Next ❯

Leave a comment