include ../_util-fns
:marked
We’ve all used a form to login, submit a help request, place an order, book a flight,
schedule a meeting and perform countless other data entry tasks.
Forms are the mainstay of business applications.
Any seasoned web developer can slap together an HTML form with all the right tags.
It's more challenging to create a cohesive data entry experience that guides the
user efficiently and effectively through the workflow behind the form.
*That* takes design skills that are, to be frank, well out of scope for this chapter.
It also takes framework support for
**two-way data binding, change tracking, validation, and error handling**
... which we shall cover in this chapter on Angular forms.
We will build a simple form from scratch, one step at a time. Along the way we'll learn
* How to build an Angular form with a component and template
* The `ngModel` two-way data binding syntax for reading and writing values to input controls
* The `ngModel` directive in combination with a form to track the change state and validity of form controls
* The Special CSS classes that follow the state of the controls and can be used to provide strong visual feedback
* How to display validation errors to users and enable/disable form controls
* How to share information across controls with template local variables
index.html
and add the following link to the <head>
.
+makeExample('forms/js/src/index.html', 'bootstrap')(format=".")
:marked
.l-main-section
:marked
## Add Powers with ***ngFor**
Our hero may choose one super power from a fixed list of Agency-approved powers.
We maintain that list internally (in `HeroFormComponent`).
We'll add a `select` to our
form and bind the options to the `powers` list using `NgFor`,
a technique we might have seen before in the [Displaying Data](./displaying-data.html) chapter.
Add the following HTML *immediately below* the *Alter Ego* group.
+makeExample('forms/js/src/app/hero-form.component.html', 'powers', 'src/app/hero-form.component.html (excerpt)')(format=".")
:marked
We are repeating the `