2017-02-22 13:09:39 -05:00
@title
Reactive Forms
@intro
Create a reactive form using FormBuilder, groups, and arrays.
@description
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
_Reactive forms_ is an Angular technique for creating forms in a _reactive_ style.
This guide explains reactive forms as you follow the steps to build a "Hero Detail Editor" form.
{@a toc}
2017-03-31 19:57:13 -04:00
2017-05-04 12:46:43 -04:00
<!--
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Contents
2017-03-31 19:57:13 -04:00
* [Introduction to reactive forms ](guide/reactive-forms#intro )
* [Setup ](guide/reactive-forms#setup )
* [Create a data model ](guide/reactive-forms#data-model )
* [Create a _reactive forms_ component ](guide/reactive-forms#create-component )
* [Create its template file ](guide/reactive-forms#create-template )
* [Import the _ReactiveFormsModule_ ](guide/reactive-forms#import )
* [Display the _HeroDetailComponent_ ](guide/reactive-forms#update )
* [Add a FormGroup ](guide/reactive-forms#formgroup )
* [Taking a look at the form model ](guide/reactive-forms#json )
* [Introduction to _FormBuilder_ ](guide/reactive-forms#formbuilder )
* [Validators.required ](guide/reactive-forms#validators )
* [Nested FormGroups ](guide/reactive-forms#grouping )
* [Inspect _FormControl_ properties ](guide/reactive-forms#properties )
* [Set form model data using _setValue_ and _patchValue_ ](guide/reactive-forms#set-data )
* [Use _FormArray_ to present an array of _FormGroups_ ](guide/reactive-forms#form-array )
* [Observe control changes ](guide/reactive-forms#observe-control )
* [Save form data ](guide/reactive-forms#save )
2017-02-22 13:09:39 -05:00
2017-05-04 12:46:43 -04:00
-->
2017-02-22 13:09:39 -05:00
Try the < live-example plnkr = "final" title = "Reactive Forms (final) in Plunker" > Reactive Forms live-example< / live-example > .
You can also run the < live-example title = "Reactive Forms Demo in Plunker" > Reactive Forms Demo< / live-example > version
and choose one of the intermediate steps from the "demo picker" at the top.
{@a intro}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Introduction to Reactive Forms
Angular offers two form-building technologies: _reactive_ forms and _template-driven_ forms.
The two technologies belong to the `@angular/forms` library
and share a common set of form control classes.
But they diverge markedly in philosophy, programming style, and technique.
They even have their own modules: the `ReactiveFormsModule` and the `FormsModule` .
### _Reactive_ forms
2017-04-26 08:11:02 -04:00
Angular _reactive_ forms facilitate a _reactive style_ of programming
2017-02-22 13:09:39 -05:00
that favors explicit management of the data flowing between
2017-04-26 08:11:02 -04:00
a non-UI _data model_ (typically retrieved from a server) and a
UI-oriented _form model_ that retains the states
and values of the HTML controls on screen. Reactive forms offer the ease
2017-02-22 13:09:39 -05:00
of using reactive patterns, testing, and validation.
With _reactive_ forms, you create a tree of Angular form control objects
2017-04-26 08:11:02 -04:00
in the component class and bind them to native form control elements in the
component template, using techniques described in this guide.
2017-02-22 13:09:39 -05:00
2017-04-26 08:11:02 -04:00
You create and manipulate form control objects directly in the
component class. As the component class has immediate access to both the data
model and the form control structure, you can push data model values into
the form controls and pull user-changed values back out. The component can
2017-02-22 13:09:39 -05:00
observe changes in form control state and react to those changes.
2017-04-26 08:11:02 -04:00
One advantage of working with form control objects directly is that value and validity updates
are [always synchronous and under your control ](guide/reactive-forms#async-vs-sync "Async vs sync" ).
2017-02-22 13:09:39 -05:00
You won't encounter the timing issues that sometimes plague a template-driven form
and reactive forms can be easier to unit test.
2017-04-26 08:11:02 -04:00
In keeping with the reactive paradigm, the component
2017-02-22 13:09:39 -05:00
preserves the immutability of the _data model_ ,
treating it as a pure source of original values.
2017-04-26 08:11:02 -04:00
Rather than update the data model directly,
the component extracts user changes and forwards them to an external component or service,
which does something with them (such as saving them)
and returns a new _data model_ to the component that reflects the updated model state.
Using reactive form directives does not require you to follow all reactive priniciples,
2017-02-22 13:09:39 -05:00
but it does facilitate the reactive programming approach should you choose to use it.
### _Template-driven_ forms
2017-03-11 10:36:40 -05:00
_Template-driven_ forms, introduced in the [Template guide ](guide/forms ), take a completely different approach.
2017-02-22 13:09:39 -05:00
2017-04-26 08:11:02 -04:00
You place HTML form controls (such as `<input>` and `<select>` ) in the component template and
bind them to _data model_ properties in the component, using directives
2017-02-22 13:09:39 -05:00
like `ngModel` .
2017-04-26 08:11:02 -04:00
You don't create Angular form control objects. Angular directives
create them for you, using the information in your data bindings.
2017-02-22 13:09:39 -05:00
You don't push and pull data values. Angular handles that for you with `ngModel` .
Angular updates the mutable _data model_ with user changes as they happen.
For this reason, the `ngModel` directive is not part of the ReactiveFormsModule.
2017-04-26 08:11:02 -04:00
While this means less code in the component class,
2017-03-11 10:36:40 -05:00
[template-driven forms are asynchronous ](guide/reactive-forms#async-vs-sync "Async vs sync" )
2017-04-26 08:11:02 -04:00
which may complicate development in more advanced scenarios.
2017-02-22 13:09:39 -05:00
{@a async-vs-sync}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### Async vs. sync
Reactive forms are synchronous. Template-driven forms are asynchronous. It's a difference that matters.
2017-04-26 08:11:02 -04:00
In reactive forms, you create the entire form control tree in code.
You can immediately update a value or drill down through the descendents of the parent form
2017-02-22 13:09:39 -05:00
because all controls are always available.
Template-driven forms delegate creation of their form controls to directives.
2017-04-26 08:11:02 -04:00
To avoid "_changed after checked_" errors,
2017-02-22 13:09:39 -05:00
these directives take more than one cycle to build the entire control tree.
That means you must wait a tick before manipulating any of the controls
from within the component class.
2017-04-26 08:11:02 -04:00
For example, if you inject the form control with a `@ViewChild(NgForm)` query and examine it in the
2017-03-31 19:57:13 -04:00
[`ngAfterViewInit` lifecycle hook ](guide/lifecycle-hooks#afterview "Lifecycle hooks guide: AfterView" ),
2017-02-22 13:09:39 -05:00
you'll discover that it has no children.
You must wait a tick, using `setTimeout` , before you can
extract a value from a control, test its validity, or set it to a new value.
2017-04-26 08:11:02 -04:00
The asynchrony of template-driven forms also complicates unit testing.
You must wrap your test block in `async()` or `fakeAsync()` to
avoid looking for values in the form that aren't there yet.
2017-02-22 13:09:39 -05:00
With reactive forms, everything is available when you expect it to be.
2017-04-26 08:11:02 -04:00
### Which is better, reactive or template-driven?
2017-02-22 13:09:39 -05:00
Neither is "better".
2017-04-26 08:11:02 -04:00
They're two different architectural paradigms,
2017-02-22 13:09:39 -05:00
with their own strengths and weaknesses.
Choose the approach that works best for you.
You may decide to use both in the same application.
2017-04-26 08:11:02 -04:00
The balance of this _reactive forms_ guide explores the _reactive_ paradigm and
concentrates exclusively on reactive forms techniques.
2017-03-11 10:36:40 -05:00
For information on _template-driven forms_ , see the [_Forms_ ](guide/forms ) guide.
2017-02-22 13:09:39 -05:00
In the next section, you'll set up your project for the reactive form demo.
2017-03-11 10:36:40 -05:00
Then you'll learn about the [Angular form classes ](guide/reactive-forms#essentials ) and how to use them in a reactive form.
2017-02-22 13:09:39 -05:00
{@a setup}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Setup
2017-04-26 08:11:02 -04:00
Follow the steps in the [_Setup_ guide ](guide/setup "Setup guide" )
2017-02-22 13:09:39 -05:00
for creating a new project folder (perhaps called `reactive-forms` )
based on the _QuickStart seed_ .
{@a data-model}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Create a data model
The focus of this guide is a reactive forms component that edits a hero.
You'll need a `hero` class and some hero data.
Create a new `data-model.ts` file in the `app` directory and copy the content below into it.
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/data-model.ts" title = "src/app/data-model.ts" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
The file exports two classes and two constants. The `Address`
and `Hero` classes define the application _data model_ .
The `heroes` and `states` constants supply the test data.
2017-02-22 13:09:39 -05:00
{@a create-component}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Create a _reactive forms_ component
2017-04-26 08:11:02 -04:00
Make a new file called
2017-02-22 13:09:39 -05:00
`hero-detail.component.ts` in the `app` directory and import these symbols:
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-1.component.ts" region = "imports" title = "src/app/hero-detail.component.ts" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Now enter the `@Component` decorator that specifies the `HeroDetailComponent` metadata:
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail.component.ts" region = "metadata" title = "src/app/hero-detail.component.ts (excerpt)" linenums = "false" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
Next, create an exported `HeroDetailComponent` class with a `FormControl` .
`FormControl` is a directive that allows you to create and manage
2017-02-22 13:09:39 -05:00
a `FormControl` instance directly.
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-1.component.ts" region = "v1" title = "src/app/hero-detail.component.ts (excerpt)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
Here you are creating a `FormControl` called `name` .
It will be bound in the template to an HTML `input` box for the hero name.
2017-02-22 13:09:39 -05:00
2017-04-26 08:11:02 -04:00
A `FormControl` constructor accepts three, optional arguments:
2017-02-22 13:09:39 -05:00
the initial data value, an array of validators, and an array of async validators.
This simple control doesn't have data or validators.
2017-04-26 08:11:02 -04:00
In real apps, most form controls have both.
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
2017-04-10 11:51:13 -04:00
< div class = "l-sub-section" >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
This guide touches only briefly on `Validators` . For an in-depth look at them,
2017-04-30 16:10:32 -04:00
read the [Form Validation ](guide/form-validation ) guide.
2017-02-22 13:09:39 -05:00
2017-04-10 11:51:13 -04:00
< / div >
2017-03-27 11:08:53 -04:00
2017-02-22 13:09:39 -05:00
{@a create-template}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Create the template
Now create the component's template, `src/app/hero-detail.component.html` , with the following markup.
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-1.component.html" region = "simple-control" title = "src/app/hero-detail.component.html" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
To let Angular know that this is the input that you want to
associate to the `name` `FormControl` in the class,
you need `[formControl]="name"` in the template on the `<input>` .
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
2017-04-10 11:51:13 -04:00
< div class = "l-sub-section" >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
Disregard the `form-control` _CSS_ class. It belongs to the
2017-04-24 14:23:45 -04:00
< a href = "http://getbootstrap.com/" title = "Bootstrap CSS" > Bootstrap CSS library< / a > ,
2017-02-22 13:09:39 -05:00
not Angular.
It _styles_ the form but in no way impacts the logic of the form.
2017-04-10 11:51:13 -04:00
< / div >
2017-03-27 11:08:53 -04:00
2017-02-22 13:09:39 -05:00
{@a import}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Import the _ReactiveFormsModule_
2017-04-26 08:11:02 -04:00
The HeroDetailComponent template uses `formControlName`
2017-02-22 13:09:39 -05:00
directive from the `ReactiveFormsModule` .
In this sample, you declare the `HeroDetailComponent` in the `AppModule` .
Therefore, do the following three things in `app.module.ts` :
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
1. Use a JavaScript `import` statement to access
2017-02-22 13:09:39 -05:00
the `ReactiveFormsModule` and the `HeroDetailComponent` .
1. Add `ReactiveFormsModule` to the `AppModule` 's `imports` list.
1. Add `HeroDetailComponent` to the declarations array.
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/app.module.ts" region = "v1" title = "src/app/app.module.ts (excerpt)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
{@a update}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Display the _HeroDetailComponent_
Revise the `AppComponent` template so it displays the `HeroDetailComponent` .
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/app.component.1.ts" title = "src/app/app.component.ts" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
{@a essentials}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### Essential form classes
It may be helpful to read a brief description of the core form classes.
2017-04-30 16:10:32 -04:00
* [_AbstractControl_ ](api/forms/AbstractControl "API Reference: AbstractControl" )
2017-04-26 08:11:02 -04:00
is the abstract base class for the three concrete form control classes:
2017-02-22 13:09:39 -05:00
`FormControl` , `FormGroup` , and `FormArray` .
It provides their common behaviors and properties, some of which are _observable_ .
2017-04-30 16:10:32 -04:00
* [_FormControl_ ](api/forms/FormControl "API Reference: FormControl" )
2017-02-22 13:09:39 -05:00
tracks the value and validity status of an _individual_ form control.
It corresponds to an HTML form control such as an input box or selector.
2017-04-30 16:10:32 -04:00
* [_FormGroup_ ](api/forms/FormGroup "API Reference: FormGroup" )
2017-02-22 13:09:39 -05:00
tracks the value and validity state of a _group_ of `AbstractControl` instances.
The group's properties include its child controls.
The top-level form in your component is a `FormGroup` .
2017-04-30 16:10:32 -04:00
* [_FormArray_ ](api/forms/FormArray "API Reference: FormArray" )
2017-02-22 13:09:39 -05:00
tracks the value and validity state of a numerically indexed _array_ of `AbstractControl` instances.
You'll learn more about these classes as you work through this guide.
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### Style the app
You used bootstrap CSS classes in the template HTML of both the `AppComponent` and the `HeroDetailComponent` .
Add the `bootstrap` _CSS stylesheet_ to the head of `index.html` :
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/index.html" region = "bootstrap" title = "index.html" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
Now that everything is wired up, the browser should display something like this:
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
< figure class = 'image-display' >
2017-05-03 08:55:00 -04:00
< img src = "generated/images/guide/reactive-forms/just-formcontrol.png" width = "400px" alt = "Single FormControl" > < / img >
2017-02-22 13:09:39 -05:00
< / figure >
{@a formgroup}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Add a FormGroup
2017-04-26 08:11:02 -04:00
Usually, if you have multiple *FormControls* , you'll want to register
2017-02-22 13:09:39 -05:00
them within a parent `FormGroup` .
2017-04-26 08:11:02 -04:00
This is simple to do. To add a `FormGroup` , add it to the imports section
2017-02-22 13:09:39 -05:00
of `hero-detail.component.ts` :
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-2.component.ts" region = "imports" title = "src/app/hero-detail.component.ts" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
In the class, wrap the `FormControl` in a `FormGroup` called `heroForm` as follows:
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-2.component.ts" region = "v2" title = "src/app/hero-detail.component.ts" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
Now that you've made changes in the class, they need to be reflected in the
2017-02-22 13:09:39 -05:00
template. Update `hero-detail.component.html` by replacing it with the following.
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-2.component.html" region = "basic-form" title = "src/app/hero-detail.component.html" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
Notice that now the single input is in a `form` element. The `novalidate`
attribute in the `<form>` element prevents the browser
from attempting native HTML validations.
2017-02-22 13:09:39 -05:00
2017-04-26 08:11:02 -04:00
`formGroup` is a reactive form directive that takes an existing
`FormGroup` instance and associates it with an HTML element.
In this case, it associates the `FormGroup` you saved as
2017-02-22 13:09:39 -05:00
`heroForm` with the form element.
2017-04-26 08:11:02 -04:00
Because the class now has a `FormGroup` , you must update the template
syntax for associating the input with the corresponding
`FormControl` in the component class.
Without a parent `FormGroup` ,
`[formControl]="name"` worked earlier because that directive
can stand alone, that is, it works without being in a `FormGroup` .
With a parent `FormGroup` , the `name` input needs the syntax
`formControlName=name` in order to be associated
with the correct `FormControl`
in the class. This syntax tells Angular to look for the parent
`FormGroup` , in this case `heroForm` , and then _inside_ that group
2017-02-22 13:09:39 -05:00
to look for a `FormControl` called `name` .
2017-03-27 11:08:53 -04:00
2017-04-10 11:51:13 -04:00
< div class = "l-sub-section" >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
Disregard the `form-group` _CSS_ class. It belongs to the
2017-04-24 14:23:45 -04:00
< a href = "http://getbootstrap.com/" title = "Bootstrap CSS" > Bootstrap CSS library< / a > ,
2017-02-22 13:09:39 -05:00
not Angular.
2017-04-26 08:11:02 -04:00
Like the `form-control` class, it _styles_ the form
2017-02-22 13:09:39 -05:00
but in no way impacts its logic.
2017-03-27 11:08:53 -04:00
2017-04-10 11:51:13 -04:00
< / div >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
The form looks great. But does it work?
When the user enters a name, where does the value go?
2017-02-22 13:09:39 -05:00
{@a json}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Taking a look at the form model
The value goes into the **_form model_** that backs the group's `FormControls` .
2017-04-26 08:11:02 -04:00
To see the form model, add the following line after the
2017-02-22 13:09:39 -05:00
closing `form` tag in the `hero-detail.component.html` :
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-3.component.html" region = "form-value-json" title = "src/app/hero-detail.component.html" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
The `heroForm.value` returns the _form model_ .
Piping it through the `JsonPipe` renders the model as JSON in the browser:
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
< figure class = 'image-display' >
2017-05-03 08:55:00 -04:00
< img src = "generated/images/guide/reactive-forms/json-output.png" width = "400px" alt = "JSON output" > < / img >
2017-02-22 13:09:39 -05:00
< / figure >
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
The initial `name` property value is the empty string.
2017-02-22 13:09:39 -05:00
Type into the _name_ input box and watch the keystokes appear in the JSON.
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
Great! You have the basics of a form.
2017-02-22 13:09:39 -05:00
2017-04-26 08:11:02 -04:00
In real life apps, forms get big fast.
2017-02-22 13:09:39 -05:00
`FormBuilder` makes form development and maintenance easier.
{@a formbuilder}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Introduction to _FormBuilder_
2017-04-26 08:11:02 -04:00
The `FormBuilder` class helps reduce repetition and
clutter by handling details of control creation for you.
2017-02-22 13:09:39 -05:00
To use `FormBuilder` , you need to import it into `hero-detail.component.ts` :
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-3a.component.ts" region = "imports" title = "src/app/hero-detail.component.ts (excerpt)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Use it now to refactor the `HeroDetailComponent` into something that's a little easier to read and write,
by following this plan:
* Explicitly declare the type of the `heroForm` property to be `FormGroup` ; you'll initialize it later.
* Inject a `FormBuilder` into the constructor.
* Add a new method that uses the `FormBuilder` to define the `heroForm` ; call it `createForm` .
* Call `createForm` in the constructor.
The revised `HeroDetailComponent` looks like this:
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-3a.component.ts" region = "v3a" title = "src/app/hero-detail.component.ts (excerpt)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
`FormBuilder.group` is a factory method that creates a `FormGroup` .
2017-04-26 08:11:02 -04:00
`FormBuilder.group` takes an object whose keys and values are `FormControl` names and their definitions.
2017-02-22 13:09:39 -05:00
In this example, the `name` control is defined by its initial data value, an empty string.
Defining a group of controls in a single object makes for a compact, readable style.
It beats writing an equivalent series of `new FormControl(...)` statements.
{@a validators}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### Validators.required
2017-04-26 08:11:02 -04:00
Though this guide doesn't go deeply into validations, here is one example that
demonstrates the simplicity of using `Validators.required` in reactive forms.
2017-02-22 13:09:39 -05:00
First, import the `Validators` symbol.
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-3.component.ts" region = "imports" title = "src/app/hero-detail.component.ts (excerpt)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
To make the `name` `FormControl` required, replace the `name`
property in the `FormGroup` with an array.
2017-02-22 13:09:39 -05:00
The first item is the initial value for `name` ;
the second is the required validator, `Validators.required` .
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-3.component.ts" region = "required" title = "src/app/hero-detail.component.ts (excerpt)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-04-10 11:51:13 -04:00
< div class = "l-sub-section" >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Reactive validators are simple, composable functions.
2017-04-26 08:11:02 -04:00
Configuring validation is harder in template-driven forms where you must wrap validators in a directive.
2017-03-27 11:08:53 -04:00
2017-04-10 11:51:13 -04:00
< / div >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -04:00
Update the diagnostic message at the bottom of the template to display the form's validity status.
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-3.component.html" region = "form-value-json" title = "src/app/hero-detail.component.html (excerpt)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
The browser displays the following:
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
< figure class = 'image-display' >
2017-05-03 08:55:00 -04:00
< img src = "generated/images/guide/reactive-forms/validators-json-output.png" width = "400px" alt = "Single FormControl" > < / img >
2017-02-22 13:09:39 -05:00
< / figure >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
`Validators.required` is working. The status is `INVALID` because the input box has no value.
2017-04-26 08:11:02 -04:00
Type into the input box to see the status change from `INVALID` to `VALID` .
2017-02-22 13:09:39 -05:00
In a real app, you'd replace the diagnosic message with a user-friendly experience.
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
Using `Validators.required` is optional for the rest of the guide.
2017-02-22 13:09:39 -05:00
It remains in each of the following examples with the same configuration.
For more on validating Angular forms, see the
2017-04-30 16:10:32 -04:00
[Form Validation ](guide/form-validation ) guide.
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### More FormControls
2017-04-26 08:11:02 -04:00
A hero has more than a name.
A hero has an address, a super power and sometimes a sidekick too.
2017-02-22 13:09:39 -05:00
2017-04-26 08:11:02 -04:00
The address has a state property. The user will select a state with a `<select>` box and you'll populate
2017-02-22 13:09:39 -05:00
the `<option>` elements with states. So import `states` from `data-model.ts` .
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-4.component.ts" region = "imports" title = "src/app/hero-detail.component.ts (excerpt)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
Declare the `states` property and add some address `FormControls` to the `heroForm` as follows.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-4.component.ts" region = "v4" title = "src/app/hero-detail.component.ts (excerpt)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
Then add corresponding markup in `hero-detail.component.html`
2017-02-22 13:09:39 -05:00
within the `form` element.
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-4.component.html" title = "src/app/hero-detail.component.html" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-04-10 11:51:13 -04:00
< div class = "alert is-helpful" >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
*Reminder*: Ignore the many mentions of `form-group` ,
2017-02-22 13:09:39 -05:00
`form-control` , `center-block` , and `checkbox` in this markup.
Those are _bootstrap_ CSS classes that Angular itself ignores.
Pay attention to the `formGroupName` and `formControlName` attributes.
2017-04-26 08:11:02 -04:00
They are the Angular directives that bind the HTML controls to the
2017-02-22 13:09:39 -05:00
Angular `FormGroup` and `FormControl` properties in the component class.
2017-04-10 11:51:13 -04:00
< / div >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
The revised template includes more text inputs, a select box for the `state` , radio buttons for the `power` ,
and a checkbox for the `sidekick` .
2017-02-22 13:09:39 -05:00
2017-04-26 08:11:02 -04:00
You must bind the option's value property with `[value]="state"` .
2017-04-10 11:51:13 -04:00
If you do not bind the value, the select shows the first option from the data model.
2017-02-22 13:09:39 -05:00
The component _class_ defines control properties without regard for their representation in the template.
You define the `state` , `power` , and `sidekick` controls the same way you defined the `name` control.
2017-04-26 08:11:02 -04:00
You tie these controls to the template HTML elements in the same way,
specifiying the `FormControl` name with the `formControlName` directive.
2017-02-22 13:09:39 -05:00
2017-04-26 08:11:02 -04:00
See the API reference for more information about
2017-04-30 16:10:32 -04:00
[radio buttons ](api/forms/RadioControlValueAccessor "API: RadioControlValueAccessor" ),
[selects ](api/forms/SelectControlValueAccessor "API: SelectControlValueAccessor" ), and
[checkboxes ](api/forms/CheckboxControlValueAccessor "API: CheckboxControlValueAccessor" ).
2017-02-22 13:09:39 -05:00
{@a grouping}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### Nested FormGroups
2017-04-26 08:11:02 -04:00
This form is getting big and unwieldy. You can group some of the related `FormControls`
into a nested `FormGroup` . The `street` , `city` , `state` , and `zip` are properties
2017-02-22 13:09:39 -05:00
that would make a good _address_ `FormGroup` .
2017-04-26 08:11:02 -04:00
Nesting groups and controls in this way allows you to
mirror the hierarchical structure of the data model
and helps track validation and state for related sets of controls.
2017-02-22 13:09:39 -05:00
You used the `FormBuilder` to create one `FormGroup` in this component called `heroForm` .
Let that be the parent `FormGroup` .
Use `FormBuilder` again to create a child `FormGroup` that encapsulates the address controls;
assign the result to a new `address` property of the parent `FormGroup` .
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-5.component.ts" region = "v5" title = "src/app/hero-detail.component.ts (excerpt)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
You’ ve changed the structure of the form controls in the component class;
2017-02-22 13:09:39 -05:00
you must make corresponding adjustments to the component template.
In `hero-detail.component.html` , wrap the address-related `FormControls` in a `div` .
Add a `formGroupName` directive to the `div` and bind it to `"address"` .
That's the property of the _address_ child `FormGroup` within the parent `FormGroup` called `heroForm` .
2017-04-26 08:11:02 -04:00
To make this change visually obvious, slip in an `<h4>` header near the top with the text, _Secret Lair_ .
2017-02-22 13:09:39 -05:00
The new _address_ HTML looks like this:
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-5.component.html" region = "add-group" title = "src/app/hero-detail.component.html (excerpt)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
After these changes, the JSON output in the browser shows the revised _form model_
2017-04-26 08:11:02 -04:00
with the nested address `FormGroup` :
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
< figure class = 'image-display' >
2017-05-03 08:55:00 -04:00
< img src = "generated/images/guide/reactive-forms/address-group.png" width = "400px" alt = "JSON output" > < / img >
2017-02-22 13:09:39 -05:00
< / figure >
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
Great! You’ ve made a group and you can see that the template
2017-02-22 13:09:39 -05:00
and the form model are talking to one another.
{@a properties}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Inspect _FormControl_ Properties
At the moment, you're dumping the entire form model onto the page.
Sometimes you're interested only in the state of one particular `FormControl` .
You can inspect an individual `FormControl` within a form by extracting it with the `.get()` method.
2017-04-26 08:11:02 -04:00
You can do this _within_ the component class or display it on the
page by adding the following to the template,
immediately after the `{{form.value | json}}` interpolation as follows:
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-5.component.html" region = "inspect-value" title = "src/app/hero-detail.component.html" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
To get the state of a `FormControl` that’ s inside a `FormGroup` , use dot notation to path to the control.
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-5.component.html" region = "inspect-child-control" title = "src/app/hero-detail.component.html" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
You can use this technique to display _any_ property of a `FormControl`
2017-02-22 13:09:39 -05:00
such as one of the following:
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
< style >
td, th {vertical-align: top}
< / style >
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
< table width = "100%" >
< col width = "10%" >
< / col >
< col width = "90%" >
< / col >
< tr >
< th >
Property
< / th >
< th >
Description
< / th >
< / tr >
< tr >
< td >
< code > myControl.value< / code >
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
the value of a `FormControl` .
< / td >
< / tr >
< tr >
< td >
< code > myControl.status< / code >
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
the validity of a `FormControl` . Possible values: `VALID` ,
2017-03-31 19:57:13 -04:00
`INVALID` , `PENDING` , or `DISABLED` .
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr >
< td >
< code > myControl.pristine< / code >
< / td >
< td >
2017-03-31 19:57:13 -04:00
`true` if the user has _not_ changed the value in the UI.
Its opposite is `myControl.dirty` .
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr >
< td >
< code > myControl.untouched< / code >
< / td >
< td >
2017-03-31 19:57:13 -04:00
`true` if the control user has not yet entered the HTML control
and triggered its blur event. Its opposite is `myControl.touched` .
2017-04-26 08:11:02 -04:00
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< / table >
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
Learn about other `FormControl` properties in the
2017-04-30 16:10:32 -04:00
[_AbstractControl_ ](api/forms/AbstractControl ) API reference.
2017-02-22 13:09:39 -05:00
2017-04-26 08:11:02 -04:00
One common reason for inspecting `FormControl` properties is to
make sure the user entered valid values.
Read more about validating Angular forms in the
2017-04-30 16:10:32 -04:00
[Form Validation ](guide/form-validation ) guide.
2017-02-22 13:09:39 -05:00
{@a data-model-form-model}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## The _data model_ and the _form model_
At the moment, the form is displaying empty values.
The `HeroDetailComponent` should display values of a hero,
possibly a hero retrieved from a remote server.
In this app, the `HeroDetailComponent` gets its hero from a parent `HeroListComponent`
The `hero` from the server is the **_data model_** .
The `FormControl` structure is the **_form model_** .
The component must copy the hero values in the _data model_ into the _form model_ .
There are two important implications:
2017-04-26 08:11:02 -04:00
1. The developer must understand how the properties of the _data model_
2017-02-22 13:09:39 -05:00
map to the properties of the _form model_ .
2. User changes flow from the DOM elements to the _form model_ , not to the _data model_ .
The form controls never update the _data model_ .
The _form_ and _data_ model structures need not match exactly.
You often present a subset of the _data model_ on a particular screen.
But it makes things easier if the shape of the _form model_ is close to the shape of the _data model_ .
In this `HeroDetailComponent` , the two models are quite close.
Recall the definition of `Hero` in `data-model.ts` :
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/data-model.ts" region = "model-classes" title = "src/app/data-model.ts (classes)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Here, again, is the component's `FormGroup` definition.
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-6.component.ts" region = "hero-form-model" title = "src/app/hero-detail.component.ts (excerpt)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
There are two significant differences between these models:
1. The `Hero` has an `id` . The form model does not because you generally don't show primary keys to users.
1. The `Hero` has an array of addresses. This form model presents only one address,
2017-03-11 10:36:40 -05:00
a choice [revisited below ](guide/reactive-forms#form-array "Form arrays" ).
2017-02-22 13:09:39 -05:00
Nonetheless, the two models are pretty close in shape and you'll see in a moment how this alignment facilitates copying the _data model_ properties
to the _form model_ with the `patchValue` and `setValue` methods.
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Take a moment to refactor the _address_ `FormGroup` definition for brevity and clarity as follows:
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-7.component.ts" region = "address-form-group" title = "src/app/hero-detail-7.component.ts" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
Also be sure to update the import from `data-model` so you can reference the `Hero` and `Address` classes:
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-7.component.ts" region = "import-address" title = "src/app/hero-detail-7.component.ts" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
{@a set-data}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Populate the form model with _setValue_ and _patchValue_
2017-04-26 08:11:02 -04:00
Previously you created a control and initialized its value at the same time.
You can also initialize or reset the values _later_ with the
2017-02-22 13:09:39 -05:00
`setValue` and `patchValue` methods.
### _setValue_
With ** `setValue` **, you assign _every_ form control value _at once_
by passing in a data object whose properties exactly match the _form model_ behind the `FormGroup` .
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-7.component.ts" region = "set-value" title = "src/app/hero-detail.component.ts (excerpt)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
The `setValue` method checks the data object thoroughly before assigning any form control values.
2017-04-26 08:11:02 -04:00
It will not accept a data object that doesn't match the FormGroup structure or is
missing values for any control in the group. This way, it can return helpful
error messages if you have a typo or if you've nested controls incorrectly.
2017-02-22 13:09:39 -05:00
`patchValue` will fail silently.
2017-04-26 08:11:02 -04:00
On the other hand,`setValue` will catch
the error and report it clearly.
2017-02-22 13:09:39 -05:00
Notice that you can _almost_ use the entire `hero` as the argument to `setValue`
because its shape is similar to the component's `FormGroup` structure.
You can only show the hero's first address and you must account for the possibility that the `hero` has no addresses at all.
This explains the conditional setting of the `address` property in the data object argument:
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-7.component.ts" region = "set-value-address" title = "src/app/hero-detail-7.component.ts" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### _patchValue_
With ** `patchValue` **, you can assign values to specific controls in a `FormGroup`
2017-04-26 08:11:02 -04:00
by supplying an object of key/value pairs for just the controls of interest.
2017-02-22 13:09:39 -05:00
This example sets only the form's `name` control.
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-6.component.ts" region = "patch-value" title = "src/app/hero-detail.component.ts (excerpt)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
With ** `patchValue` ** you have more flexibility to cope with wildly divergent data and form models.
2017-04-26 08:11:02 -04:00
But unlike `setValue` , `patchValue` cannot check for missing control
2017-02-22 13:09:39 -05:00
values and does not throw helpful errors.
### When to set form model values (_ngOnChanges_)
Now you know _how_ to set the _form model_ values. But _when_ do you set them?
The answer depends upon when the component gets the _data model_ values.
2017-03-11 10:36:40 -05:00
The `HeroDetailComponent` in this reactive forms sample is nested within a _master/detail_ `HeroListComponent` ([discussed below](guide/reactive-forms#hero-list)).
2017-02-22 13:09:39 -05:00
The `HeroListComponent` displays hero names to the user.
When the user clicks on a hero, the list component passes the selected hero into the `HeroDetailComponent`
by binding to its `hero` input property.
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-list.component.1.html" title = "hero-list.component.html (simplified)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
In this approach, the value of `hero` in the `HeroDetailComponent` changes
2017-02-22 13:09:39 -05:00
every time the user selects a new hero.
2017-04-12 15:53:18 -04:00
You should call _setValue_ in the [ngOnChanges ](guide/lifecycle-hooks#onchanges )
2017-02-22 13:09:39 -05:00
hook, which Angular calls whenever the input `hero` property changes
as the following steps demonstrate.
2017-03-27 11:08:53 -04:00
First, import the `OnChanges` and `Input` symbols in `hero-detail.component.ts` .
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-6.component.ts" region = "import-input" title = "src/app/hero-detail.component.ts (core imports)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Add the `hero` input property.
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-6.component.ts" region = "hero" title = "src/app/hero-detail-6.component.ts" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Add the `ngOnChanges` method to the class as follows:
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-7.component.ts" region = "ngOnChanges-1" title = "src/app/hero-detail.component.ts (ngOnchanges)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### _reset_ the form flags
2017-04-26 08:11:02 -04:00
You should reset the form when the hero changes so that
control values from the previous hero are cleared and
2017-02-22 13:09:39 -05:00
status flags are restored to the _pristine_ state.
You could call `reset` at the top of `ngOnChanges` like this.
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-7.component.ts" region = "reset" title = "src/app/hero-detail-7.component.ts" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-04-28 18:33:23 -04:00
The `reset` method has an optional `state` value so you can reset the flags _and_ the control values at the same time.
2017-04-26 08:11:02 -04:00
Internally, `reset` passes the argument to `setValue` .
2017-02-22 13:09:39 -05:00
A little refactoring and `ngOnChanges` becomes this:
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-7.component.ts" region = "ngOnChanges" title = "src/app/hero-detail.component.ts (ngOnchanges - revised)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
{@a hero-list}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### Create the _HeroListComponent_ and _HeroService_
The `HeroDetailComponent` is a nested sub-component of the `HeroListComponent` in a _master/detail_ view.
Together they look a bit like this:
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
< figure class = 'image-display' >
2017-05-03 08:55:00 -04:00
< img src = "generated/images/guide/reactive-forms/hero-list.png" width = "420px" alt = "HeroListComponent" > < / img >
2017-02-22 13:09:39 -05:00
< / figure >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
The `HeroListComponent` uses an injected `HeroService` to retrieve heroes from the server
and then presents those heroes to the user as a series of buttons.
2017-04-26 08:11:02 -04:00
The `HeroService` emulates an HTTP service.
It returns an `Observable` of heroes that resolves after a short delay,
both to simulate network latency and to indicate visually
2017-02-22 13:09:39 -05:00
the necessarily asynchronous nature of the application.
When the user clicks on a hero,
the component sets its `selectedHero` property which
is bound to the `hero` input property of the `HeroDetailComponent` .
The `HeroDetailComponent` detects the changed hero and re-sets its form
with that hero's data values.
A "Refresh" button clears the hero list and the current selected hero before refetching the heroes.
The remaining `HeroListComponent` and `HeroService` implementation details are not relevant to understanding reactive forms.
The techniques involved are covered elsewhere in the documentation, including the _Tour of Heroes_
2017-03-31 19:57:13 -04:00
[here ](tutorial/toh-pt3 "ToH: Multiple Components" ) and [here ](tutorial/toh-pt4 "ToH: Services" ).
2017-02-22 13:09:39 -05:00
2017-04-26 08:11:02 -04:00
If you're coding along with the steps in this reactive forms tutorial,
create the pertinent files based on the
[source code displayed below ](guide/reactive-forms#source-code "Reactive Forms source code" ).
2017-03-06 05:43:33 -05:00
Notice that `hero-list.component.ts` imports `Observable` and `finally` while `hero.service.ts` imports `Observable` , `of` ,
2017-04-26 08:11:02 -04:00
and `delay` from `rxjs` .
2017-02-22 13:09:39 -05:00
Then return here to learn about _form array_ properties.
{@a form-array}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Use _FormArray_ to present an array of _FormGroups_
So far, you've seen `FormControls` and `FormGroups` .
A `FormGroup` is a named object whose property values are `FormControls` and other `FormGroups` .
Sometimes you need to present an arbitrary number of controls or groups.
For example, a hero may have zero, one, or any number of addresses.
The `Hero.addresses` property is an array of `Address` instances.
An _address_ `FormGroup` can display one `Address` .
An Angular `FormArray` can display an array of _address_ `FormGroups` .
To get access to the `FormArray` class, import it into `hero-detail.component.ts` :
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-8.component.ts" region = "imports" title = "src/app/hero-detail.component.ts (excerpt)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
To _work_ with a `FormArray` you do the following:
1. Define the items (`FormControls` or `FormGroups` ) in the array.
1. Initialize the array with items created from data in the _data model_ .
1. Add and remove items as the user requires.
In this guide, you define a `FormArray` for `Hero.addresses` and
let the user add or modify addresses (removing addresses is your homework).
You’ ll need to redefine the form model in the `HeroDetailComponent` constructor,
2017-04-26 08:11:02 -04:00
which currently only displays the first hero address in an _address_ `FormGroup` .
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-7.component.ts" region = "address-form-group" title = "src/app/hero-detail-7.component.ts" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### From _address_ to _secret lairs_
From the user's point of view, heroes don't have _addresses_ .
_Addresses_ are for mere mortals. Heroes have _secret lairs_ !
Replace the _address_ `FormGroup` definition with a _secretLairs_ `FormArray` definition:
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-8.component.ts" region = "secretLairs-form-array" title = "src/app/hero-detail-8.component.ts" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-04-10 11:51:13 -04:00
< div class = "alert is-helpful" >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Changing the form control name from `address` to `secretLairs` drives home an important point:
the _form model_ doesn't have to match the _data model_ .
2017-04-26 08:11:02 -04:00
Obviously there has to be a relationship between the two.
2017-02-22 13:09:39 -05:00
But it can be anything that makes sense within the application domain.
_Presentation_ requirements often differ from _data_ requirements.
The reactive forms approach both emphasizes and facilitates this distinction.
2017-04-10 11:51:13 -04:00
< / div >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### Initialize the "secretLairs" _FormArray_
The default form displays a nameless hero with no addresses.
You need a method to populate (or repopulate) the _secretLairs_ with actual hero addresses whenever
the parent `HeroListComponent` sets the `HeroListComponent.hero` input property to a new `Hero` .
The following `setAddresses` method replaces the _secretLairs_ `FormArray` with a new `FormArray` ,
initialized by an array of hero address `FormGroups` .
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-8.component.ts" region = "set-addresses" title = "src/app/hero-detail-8.component.ts" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Notice that you replace the previous `FormArray` with the ** `FormGroup.setControl` method**, not with `setValue` .
You're replacing a _control_ , not the _value_ of a control.
Notice also that the _secretLairs_ `FormArray` contains ** `FormGroups` **, not `Addresses` .
### Get the _FormArray_
The `HeroDetailComponent` should be able to display, add, and remove items from the _secretLairs_ `FormArray` .
Use the `FormGroup.get` method to acquire a reference to that `FormArray` .
Wrap the expression in a `secretLairs` convenience property for clarity and re-use.
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-8.component.ts" region = "get-secret-lairs" title = "src/app/hero-detail.component.ts (secretLayers property)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
### Display the _FormArray_
2017-02-22 13:09:39 -05:00
The current HTML template displays a single _address_ `FormGroup` .
Revise it to display zero, one, or more of the hero's _address_ `FormGroups` .
2017-04-26 08:11:02 -04:00
This is mostly a matter of wrapping the previous template HTML for an address in a `<div>` and
2017-02-22 13:09:39 -05:00
repeating that `<div>` with `*ngFor` .
The trick lies in knowing how to write the `*ngFor` . There are three key points:
2017-04-26 08:11:02 -04:00
1. Add another wrapping `<div>` , around the `<div>` with `*ngFor` , and
2017-02-22 13:09:39 -05:00
set its `formArrayName` directive to `"secretLairs"` .
2017-04-26 08:11:02 -04:00
This step establishes the _secretLairs_ `FormArray` as the context for form controls in the inner, repeated HTML template.
2017-02-22 13:09:39 -05:00
2017-04-26 08:11:02 -04:00
1. The source of the repeated items is the `FormArray.controls` , not the `FormArray` itself.
2017-02-22 13:09:39 -05:00
Each control is an _address_ `FormGroup` , exactly what the previous (now repeated) template HTML expected.
1. Each repeated `FormGroup` needs a unique `formGroupName` which must be the index of the `FormGroup` in the `FormArray` .
You'll re-use that index to compose a unique label for each address.
Here's the skeleton for the _secret lairs_ section of the HTML template:
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-8.component.html" region = "form-array-skeleton" title = "src/app/hero-detail.component.html (*ngFor)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Here's the complete template for the _secret lairs_ section:
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-8.component.html" region = "form-array" title = "src/app/hero-detail.component.html (excerpt)" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### Add a new lair to the _FormArray_
2017-04-26 08:11:02 -04:00
Add an `addLair` method that gets the _secretLairs_ `FormArray` and appends a new _address_ `FormGroup` to it.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-8.component.ts" region = "add-lair" title = "src/app/hero-detail.component.ts (addLair method)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Place a button on the form so the user can add a new _secret lair_ and wire it to the component's `addLair` method.
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-8.component.html" region = "add-lair" title = "src/app/hero-detail.component.html (addLair button)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-04-10 11:51:13 -04:00
< div class = "alert is-important" >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
Be sure to **add the `type="button"` attribute** .
2017-02-22 13:09:39 -05:00
In fact, you should always specify a button's `type` .
Without an explict type, the button type defaults to "submit".
When you later add a _form submit_ action, every "submit" button triggers the submit action which
might do something like save the current changes.
You do not want to save changes when the user clicks the _Add a Secret Lair_ button.
2017-04-10 11:51:13 -04:00
< / div >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### Try it!
Back in the browser, select the hero named "Magneta".
"Magneta" doesn't have an address, as you can see in the diagnostic JSON at the bottom of the form.
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
< figure class = 'image-display' >
2017-05-03 08:55:00 -04:00
< img src = "generated/images/guide/reactive-forms/addresses-array.png" width = "400px" alt = "JSON output of addresses array" > < / img >
2017-02-22 13:09:39 -05:00
< / figure >
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
Click the "_Add a Secret Lair_" button.
2017-02-22 13:09:39 -05:00
A new address section appears. Well done!
### Remove a lair
This example can _add_ addresses but it can't _remove_ them.
For extra credit, write a `removeLair` method and wire it to a button on the repeating address HTML.
{@a observe-control}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Observe control changes
Angular calls `ngOnChanges` when the user picks a hero in the parent `HeroListComponent` .
Picking a hero changes the `HeroDetailComponent.hero` input property.
Angular does _not_ call `ngOnChanges` when the user modifies the hero's _name_ or _secret lairs_ .
2017-04-26 08:11:02 -04:00
Fortunately, you can learn about such changes by subscribing to one of the form control properties
2017-02-22 13:09:39 -05:00
that raises a change event.
These are properties, such as `valueChanges` , that return an RxJS `Observable` .
You don't need to know much about RxJS `Observable` to monitor form control values.
Add the following method to log changes to the value of the _name_ `FormControl` .
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail.component.ts" region = "log-name-change" title = "src/app/hero-detail.component.ts (logNameChange)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Call it in the constructor, after creating the form.
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail-8.component.ts" region = "ctor" title = "src/app/hero-detail-8.component.ts" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
The `logNameChange` method pushes name-change values into a `nameChangeLog` array.
Display that array at the bottom of the component template with this `*ngFor` binding:
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail.component.html" region = "name-change-log" title = "src/app/hero-detail.component.html (Name change log)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Return to the browser, select a hero (e.g, "Magneta"), and start typing in the _name_ input box.
You should see a new name in the log after each keystroke.
### When to use it
An interpolation binding is the easier way to _display_ a name change.
2017-04-26 08:11:02 -04:00
Subscribing to an observable form control property is handy for triggering
2017-02-22 13:09:39 -05:00
application logic _within_ the component class.
{@a save}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Save form data
The `HeroDetailComponent` captures user input but it doesn't do anything with it.
In a real app, you'd probably save those hero changes.
In a real app, you'd also be able to revert unsaved changes and resume editing.
After you implement both features in this section, the form will look like this:
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
< figure class = 'image-display' >
2017-05-03 08:55:00 -04:00
< img src = "generated/images/guide/reactive-forms/save-revert-buttons.png" width = "389px" alt = "Form with save & revert buttons" > < / img >
2017-02-22 13:09:39 -05:00
< / figure >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### Save
In this sample application, when the user submits the form,
the `HeroDetailComponent` will pass an instance of the hero _data model_
to a save method on the injected `HeroService` .
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail.component.ts" region = "on-submit" title = "src/app/hero-detail.component.ts (onSubmit)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
This original `hero` had the pre-save values. The user's changes are still in the _form model_ .
So you create a new `hero` from a combination of original hero values (the `hero.id` )
2017-04-26 08:11:02 -04:00
and deep copies of the changed form model values, using the `prepareSaveHero` helper.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail.component.ts" region = "prepare-save-hero" title = "src/app/hero-detail.component.ts (prepareSaveHero)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-04-10 11:51:13 -04:00
< div class = "l-sub-section" >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Address deep copy**
Had you assigned the `formModel.secretLairs` to `saveHero.addresses` (see line commented out),
2017-04-26 08:11:02 -04:00
the addresses in `saveHero.addresses` array would be the same objects
2017-02-22 13:09:39 -05:00
as the lairs in the `formModel.secretLairs` .
A user's subsequent changes to a lair street would mutate an address street in the `saveHero` .
The `prepareSaveHero` method makes copies of the form model's `secretLairs` objects so that can't happen.
2017-03-27 11:08:53 -04:00
2017-04-10 11:51:13 -04:00
< / div >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### Revert (cancel changes)
The user cancels changes and reverts the form to the original state by pressing the _Revert_ button.
Reverting is easy. Simply re-execute the `ngOnChanges` method that built the _form model_ from the original, unchanged `hero` _data model_ .
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail.component.ts" region = "revert" title = "src/app/hero-detail.component.ts (revert)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### Buttons
Add the "Save" and "Revert" buttons near the top of the component's template:
2017-03-31 19:57:13 -04:00
< code-example path = "reactive-forms/src/app/hero-detail.component.html" region = "buttons" title = "src/app/hero-detail.component.html (Save and Revert buttons)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
The buttons are disabled until the user "dirties" the form by changing a value in any of its form controls (`heroForm.dirty`).
Clicking a button of type `"submit"` triggers the `ngSubmit` event which calls the component's `onSubmit` method.
Clicking the revert button triggers a call to the component's `revert` method.
Users now can save or revert changes.
This is the final step in the demo.
Try the < live-example plnkr = "final" title = "Reactive Forms (final) in Plunker" > < / live-example > .
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Conclusion
This page covered:
2017-03-31 19:57:13 -04:00
* How to create a reactive form component and its corresponding template.
* How to use `FormBuilder` to simplify coding a reactive form.
* Grouping `FormControls` .
* Inspecting `FormControl` properties.
* Setting data with `patchValue` and `setValue` .
* Adding groups dynamically with `FormArray` .
* Observing changes to the value of a `FormControl` .
* Saving form changes.
2017-02-22 13:09:39 -05:00
{@a source-code}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
The key files of the final version are as follows:
2017-03-27 11:08:53 -04:00
< code-tabs >
< code-pane title = "src/app/app.component.ts" path = "reactive-forms/src/app/app.component.ts" >
< / code-pane >
< code-pane title = "src/app/app.module.ts" path = "reactive-forms/src/app/app.module.ts" >
< / code-pane >
< code-pane title = "src/app/hero-detail.component.ts" path = "reactive-forms/src/app/hero-detail.component.ts" >
< / code-pane >
< code-pane title = "src/app/hero-detail.component.html" path = "reactive-forms/src/app/hero-detail.component.html" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "src/app/hero-list.component.html" path = "reactive-forms/src/app/hero-list.component.html" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "src/app/hero-list.component.ts" path = "reactive-forms/src/app/hero-list.component.ts" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "src/app/data-model.ts" path = "reactive-forms/src/app/data-model.ts" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "src/app/hero.service.ts" path = "reactive-forms/src/app/hero.service.ts" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-tabs >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
You can download the complete source for all steps in this guide
2017-04-24 14:23:45 -04:00
from the < live-example title = "Reactive Forms Demo in Plunker" > Reactive Forms Demo< / live-example > live example.