Runoob Tutorial ArchiveEnglish

AngularJS form

AngularJS form


AngularJS form is a collection of input controls.


HTML control

The following HTML input elements are called HTML controls:


Data binding

The Input control uses the ng-model directive to implement data binding.

<

input

type

=

"

text

"

ng-model

=

"

firstname

"

The application has an attribute named firstname using the above code.

It binds to your application using the ng-model directive.

The firstname property can be used in the controller:

instance

var

app

=

angular

.

module

(

'

myApp

'

,

[

]

)

;

app

.

controller

(

'

formCtrl

'

,

function

(

$

scope

)

{

$

scope

.

firstname

=

"

John

"

;

}

)

;

Try it »

You can also use it in other parts of the application:

Instance

<

form

First Name:

<

input

type

=

"

text

"

ng-model

=

"

firstname

"

</

form

<

h1

You entered: {{firstname}}

</

h1

Try it »


Checkbox

The value of the checkbox is true or false, and it can be bound using the ng-model directive; its value can be used in the application:

Instance

When the checkbox is selected, the content of the h1 tag is displayed:

<

form

Check to show a header:

<

input

type

=

"

checkbox

"

ng-model

=

"

myVar

"

</

form

<

h1

ng-show

=

"

myVar

"

My Header

</

h1

Try it »


Radio button

We can use ng-model to bind radio buttons to your application.

Radio buttons using the same ng-model can have different values, but only the value of the selected radio button will be used.

Instance

Display information based on the selected radio button:

<

form

Select an option:

<

input

type

=

"

radio

"

ng-model

=

"

myVar

"

value

=

"

dogs

"

Dogs

<

input

type

=

"

radio

"

ng-model

=

"

myVar

"

value

=

"

tuts

"

Tutorials

<

input

type

=

"

radio

"

ng-model

=

"

myVar

"

value

=

"

cars

"

Cars

</

form

Try it »

myVar's value can be dogs, tuts, or cars.


Dropdown menu

You can bind a dropdown menu to your application using the ng-model directive.

The value of the ng-model attribute is the option you select in the dropdown menu:

Instance

Display information based on the selected dropdown menu option:

<

form

Select an option:

<

select

ng-model

=

"

myVar

"

<

option

value

=

"

"

<

option

value

=

"

dogs

"

Dogs

<

option

value

=

"

tuts

"

Tutorials

<

option

value

=

"

cars

"

Cars

</

select

</

form

Try it »

The value of myVar can be dogs, tuts, or cars.

HTML form

HTML forms usually exist with HTML controls.


AngularJS form instance

form = {{user}}

master = {{master}}


Application code

<

div

ng-app

=

"

myApp

"

ng-controller

=

"

formCtrl

"

<

form

novalidate

First Name:

<

br

<

input

type

=

"

text

"

ng-model

=

"

user.firstName

"

<

br

Last Name:

<

br

<

input

type

=

"

text

"

ng-model

=

"

user.lastName

"

<

br

<

br

<

button

ng-click

=

"

reset()

"

RESET

</

button

</

form

<

p

form = {{user}}

</

p

<

p

master = {{master}}

</

p

</

div

<

script

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(); });

</

script

Try it »

Note novalidateThis attribute was added in HTML5. It disables the browser's default validation.

Instance parsing

ng-appThe directive defines an AngularJS application.

ng-controllerThe directive defines the application controller.

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

formCtrlThe function setsthe initial value of the masterobject and definesreset()Method.

reset()The method setsuserobject equal tomasterobject.

ng-clickThe instruction calledthe reset()method, and it is called when the button is clicked.

The novalidate attribute is not required in the application, but you need to use it in AngularJS forms to override standard HTML5 validation.