docs(forms): move template reference var higher in doc (#3521)

This commit is contained in:
Kapunahele Wong 2017-04-11 22:21:47 -04:00 committed by Ward Bell
parent 10dd5b8bf0
commit 01f0f6dec0
2 changed files with 38 additions and 21 deletions

View File

@ -150,7 +150,9 @@
<!-- #docregion phase2--> <!-- #docregion phase2-->
<div class="container"> <div class="container">
<h1>Hero Form</h1> <h1>Hero Form</h1>
<form> <!-- #docregion template-variable-->
<form #heroForm="ngForm">
<!-- #enddocregion template-variable-->
<!-- #docregion ngModel-2--> <!-- #docregion ngModel-2-->
{{diagnostic}} {{diagnostic}}
<div class="form-group"> <div class="form-group">

View File

@ -223,6 +223,12 @@ figure.image-display
*You're not using Angular yet*. There are no bindings or extra directives, just layout. *You're not using Angular yet*. There are no bindings or extra directives, just layout.
.l-sub-section
:marked
In template driven forms, if you've imported `FormsModule`, you don't have to do anything
to the `<form>` tag in order to make use of `FormsModule`. Continue on to see how this works.
:marked
The `container`, `form-group`, `form-control`, and `btn` classes The `container`, `form-group`, `form-control`, and `btn` classes
come from [Twitter Bootstrap](http://getbootstrap.com/css/). These classes are purely cosmetic. come from [Twitter Bootstrap](http://getbootstrap.com/css/). These classes are purely cosmetic.
Bootstrap gives the form a little style. Bootstrap gives the form a little style.
@ -292,6 +298,31 @@ figure.image-display
:marked :marked
Focus on the binding syntax: `[(ngModel)]="..."`. Focus on the binding syntax: `[(ngModel)]="..."`.
You need one more addition to display the data. Declare
a template variable for the form. Update the `<form>` tag with
`#heroForm="ngForm"` as follows:
+makeExcerpt('src/app/hero-form.component.html (excerpt)', 'template-variable')
:marked
The variable `heroForm` is now a reference to the `NgForm` directive that governs the form as a whole.
.l-sub-section#ngForm
:marked
### The _NgForm_ directive
What `NgForm` directive?
You didn't add an [NgForm](../api/forms/index/NgForm-directive.html) directive.
Angular did. Angular automatically creates and attaches an `NgForm` directive to the `<form>` tag.
The `NgForm` directive supplements the `form` element with additional features.
It holds the controls you created for the elements with an `ngModel` directive
and `name` attribute, and monitors their properties, including their validity.
It also has its own `valid` property which is true only *if every contained
control* is valid.
:marked
If you ran the app now and started typing in the *Name* input box, If you ran the app now and started typing in the *Name* input box,
adding and deleting characters, you'd see them appear and disappear adding and deleting characters, you'd see them appear and disappear
from the interpolated text. from the interpolated text.
@ -321,7 +352,7 @@ figure.image-display
Internally, Angular creates `FormControl` instances and Internally, Angular creates `FormControl` instances and
registers them with an `NgForm` directive that Angular attached to the `<form>` tag. registers them with an `NgForm` directive that Angular attached to the `<form>` tag.
Each `FormControl` is registered under the name you assigned to the `name` attribute. Each `FormControl` is registered under the name you assigned to the `name` attribute.
Read more in [The NgForm directive](#ngForm), later in this page. Read more in the previous section, [The NgForm directive](#ngForm).
:marked :marked
Add similar `[(ngModel)]` bindings and `name` attributes to *Alter Ego* and *Hero Power*. Add similar `[(ngModel)]` bindings and `name` attributes to *Alter Ego* and *Hero Power*.
@ -544,25 +575,9 @@ figure.image-display
+makeExcerpt('forms/ts/src/app/hero-form.component.html (ngSubmit)') +makeExcerpt('forms/ts/src/app/hero-form.component.html (ngSubmit)')
:marked :marked
You added something extra at the end. You defined a You'd already defined a template reference variable,
template reference variable, `#heroForm`, and initialized it with the value "ngForm". `#heroForm`, and initialized it with the value "ngForm".
Now, use that variable to access the form with the Submit button.
The variable `heroForm` is now a reference to the `NgForm` directive that governs the form as a whole.
.l-sub-section#ngForm
:marked
### The _NgForm_ directive
What `NgForm` directive?
You didn't add an [NgForm](../api/forms/index/NgForm-directive.html) directive.
Angular did. Angular automatically creates and attaches an `NgForm` directive to the `<form>` tag.
The `NgForm` directive supplements the `form` element with additional features.
It holds the controls you created for the elements with an `ngModel` directive
and `name` attribute, and monitors their properties, including their validity.
It also has its own `valid` property which is true only *if every contained
control* is valid.
:marked :marked
You'll bind the form's overall validity via You'll bind the form's overall validity via