2015-10-16 20:39:30 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								include ../../../../_includes/_util-fns
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  We’ 
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  schedule a meeting and perform countless other data entry tasks.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Forms are the mainstay of business applications.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  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.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  *That* takes design skills that are, to be frank, well out of scope for this chapter.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  It also takes framework support for
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  **two-way data binding, change tracking, validation, and error handling**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  ... which we shall cover in this chapter on Angular forms.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  We will build a simple form from scratch, one step at a time. Along the way we'll learn
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  - How to build an Angular form with a component and template
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  - The `ngModel` two-way data binding syntax for reading and writing values to input controls
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  - The `ngControl` directive to track the change state and validity of form controls
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  - The special CSS classes that `ngControl` adds to form controls and how we can use them to provide strong visual feedback
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  - How to display validation errors to users and enable/disable form controls
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  - How to share information across controls with template local variables
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 21:21:49 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  [Live Example](/resources/live-examples/forms/ts/plnkr.html)
							 
						 
					
						
							
								
									
										
										
										
											2015-10-16 20:39:30 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  ## Template-Driven Forms
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  Many of us will build forms by writing templates in the Angular [template syntax](./template-syntax.html) with
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  the form-specific directives and techniques described in this chapter.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								.l-sub-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  :marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    That's not the only way to create a form but it's the way we'll cover in this chapter.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-12-16 07:56:00 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  We can build almost any form we need with an Angular template — login forms, contact forms ... pretty much any business forms.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  We can lay out the controls creatively, bind them to data, specify validation rules and display validation errors,
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  conditionally enable or disable specific controls, trigger built-in visual feedback, and much more.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:18:44 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  It will be pretty easy because Angular handles many of the repetitive, boiler plate tasks we'd
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  otherwise wrestle with ourselves.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  We'll discuss and learn to build the following template-driven form:
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								figure.image-display
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:18:44 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  img(src="/resources/images/devguide/forms/hero-form-1.png" width="400px" alt="Clean Form")
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Here at the *Hero Employment Agency* we use this form to maintain personal information about the
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  heroes in our stable. Every hero needs a job. It's our company mission to match the right hero with the right crisis!
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  Two of the three fields on this form are required. Required fields have a green bar on the left to make them easy to spot.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  If we delete the hero name, the form displays a validation error in an attention grabbing style:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								figure.image-display
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:18:44 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  img(src="/resources/images/devguide/forms/hero-form-2.png" width="400px" alt="Invalid, Name Required")
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Note that the submit button is disabled and the "required" bar to the left of the input control changed from green to red.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								.l-sub-section
							 
						 
					
						
							
								
									
										
										
										
											2016-01-03 16:51:09 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  p We'll customize the colors and location of the "required" bar with standard CSS.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  We will build this form in the following sequence of small steps
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  1. Create the `Hero` model class
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  1. Create the component that controls the form
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  1. Create a template with the initial form layout
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  1. Add the **ngModel** directive to each form input control
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  1. Add the **ngControl** directive to each form input control
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  1. Add custom CSS to provide visual feedback
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  1. Show and hide validation error messages
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  1. Handle form submission with **ngSubmit**
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  1. Disable the form’ 
							 
						 
					
						
							
								
									
										
										
										
											2015-11-07 20:54:31 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-11-07 20:54:31 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  ## Setup
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  Create a new project folder (`angular2-forms`) and follow the steps in the [QuickStart](../quickstart.html).
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  ## Create the Hero Model Class
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  As users enter form data, we capture their changes and update an instance of a model.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  We can't layout the form until we know what the model looks like.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  A model can be as simple as a "property bag" that holds facts about a thing of application importance.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  That describes well our `Hero` class with its three required fields (`id`, `name`, `power`)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  and one optional field (`alterEgo`).
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Create a new file in the app folder called `hero.ts` and give it the following class definition:
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								+makeExample('forms/ts/app/hero.ts', null, 'app/hero.ts')
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  It's an anemic model with few requirements and no behavior. Perfect for our demo.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  The TypeScript compiler generates a public field for each `public` constructor parameter and
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  assigns the parameter’ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  The `alterEgo` is optional and the constructor lets us omit it; note the (?) in `alterEgo?`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  We can create a new hero like this:
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								code-example(format="").
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  let myHero =  new Hero(42, 'SkyDog', 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								                         'Fetch any object at any distance', 'Leslie Rollover');
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  console.log('My hero is called ' + myHero.name); // "My hero is called SkyDog"
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  ## Create a Form component
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  An Angular form has two parts: an HTML-based template and a code-based Component to handle data and user interactions.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  We begin with the Component because it states, in brief, what the Hero editor can do.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Create a new file called `hero-form.component.ts` and give it the following definition:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								+makeExample('forms/ts/app/hero-form.component.ts', 'first', 'app/hero-form.component.ts')
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  There’ 
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  Understanding this component requires only the Angular 2 concepts we’ 
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  1. We import the `Component` decorator from the Angular library as we usually do.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  1. The `@Component` selector value of "hero-form" means we can drop this form in a parent template with a `<hero-form>` tag.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  1. The `templateUrl` property points to a separate file for template HTML called `hero-form.component.html`.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  1. We defined dummy data for `model` and `powers` as befits a demo.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Down the road, we can inject a data service to get and save real data
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  or perhaps expose these properties as [inputs and outputs](./template-syntax.html#inputs-outputs) for binding to a
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  parent component. None of this concerns us now and these future changes won't affect our form.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  1. We threw in a `diagnostic` property at the end to return a JSON representation of our model.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  It'll help us see what we're doing during our development; we've left ourselves a cleanup note to discard it later.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Why don't we write the template inline in the component file as we often do
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  elsewhere in the Developer Guide?
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  There is no “right” answer for all occasions. We like inline templates when they are short.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Most form templates won't be short. TypeScript and JavaScript files generally aren't the best place to
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  write (or read) large stretches of HTML and few editors are much help with files that have a mix of HTML and code.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  We also like short files with a clear and obvious purpose like this one.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  We made a good choice to put the HTML template elsewhere. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  We'll write that template in a moment. Before we do, we'll take a step back
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  and revise the `app.component.ts` to make use of our new `HeroFormComponent`.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-07 20:54:31 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  ## Revise the *app.component.ts*
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  `app.component.ts` is the application's root component. It will host our new `HeroFormComponent`.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-07 20:54:31 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Replace the contents of the "QuickStart" version with the following:
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								+makeExample('forms/ts/app/app.component.ts', null, 'app/app.component.ts')
							 
						 
					
						
							
								
									
										
										
										
											2015-11-07 20:54:31 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-11-07 20:54:31 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								.l-sub-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  :marked
							 
						 
					
						
							
								
									
										
										
										
											2015-11-07 20:54:31 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    There are only three changes:
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-07 20:54:31 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    1. We import the new `HeroFormComponent`.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-16 15:50:11 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    1. The `template` is simply the new element tag identified by the component's `selector` property.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-21 20:05:50 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    1. The `directives` array tells Angular that our template depends upon the `HeroFormComponent`
							 
						 
					
						
							
								
									
										
										
										
											2015-11-07 20:54:31 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    which is itself a Directive (as are all Components).
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  ## Create an initial HTML Form Template
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Create a new template file called `hero-form.component.html` and give it the following definition:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								+makeExample('forms/ts/app/hero-form.component.html', 'start', 'app/hero-form.component.html')
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  That is plain old HTML 5. We're presenting two of the `Hero` fields, `name` and `alterEgo`, and
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  opening them up for user input in input boxes.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  The *Name* `<input>` control has the HTML5 `required` attribute;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  the *Alter Ego* `<input>` control does not because `alterEgo` is optional.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  We've got a *Submit* button at the bottom with some classes on it.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  **We are not using Angular yet**. There are no bindings. No extra directives. Just layout.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  The `container`,`form-group`, `form-control`, and `btn` classes 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  come from [Twitter Boostrap](http://getbootstrap.com/css/). Purely cosmetic.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  We're using Bootstrap to gussy up our form.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Hey, what's a form without a little style!
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								.callout.is-important
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  header Angular Forms Do Not Require A Style Library
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  :marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    Angular makes no use of the `container`, `form-group`, `form-control`, and `btn` classes or
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    the styles of any external library. Angular apps can use any CSS library
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    ... or none at all.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:30:15 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  Let's add the stylesheet.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								ol
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  li Open a terminal window in the application root folder and enter the command:
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    code-example(language="html" escape="html").
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								      npm install bootstrap --save
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  li Open <code>index.html</code> and add the following link to the <code><head></code>.
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								      +makeExample('forms/ts/index.html', 'bootstrap')(format=".")
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  ## Add Powers with ***ngFor**
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Our hero may choose one super power from a fixed list of Agency-approved powers.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  We maintain that list internally (in `HeroFormComponent`).
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  We'll add a `select` to our
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  form and bind the options to the `powers` list using `NgFor`,
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  a technique we might have seen before in the [Displaying Data](./displaying-data.html) chapter.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Add the following HTML *immediately below* the *Alter Ego* group.
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								+makeExample('forms/ts/app/hero-form.component.html', 'powers', 'app/hero-form.component.html (excerpt)')(format=".")
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  We are repeating the `<options>` tag for each power in the list of Powers.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  The `#p` local template variable is a different power in each iteration;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  we display its name using the interpolation syntax with the double-curly-braces.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-14 20:05:13 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								<a id="ngModel"></a>
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  ## Two-way data binding with ***ngModel**
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Running the app right now would be disappointing.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								figure.image-display
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:18:44 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  img(src="/resources/images/devguide/forms/hero-form-3.png" width="400px" alt="Early form with no binding")
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  We don't see hero data because we are not binding to the `Hero` yet.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  We know how to do that from earlier chapters.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  [Displaying Data](./displaying-data.html) taught us Property Binding.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  [User Input](./user-input.html) showed us how to listen for DOM events with an
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  Event Binding and how to update a component property with the displayed value.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  Now we need to display, listen, and extract at the same time.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  We could use those techniques again in our form.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  Instead we'll introduce something new, the `NgModel` directive, that
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  makes binding our form to the model super-easy.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Find the `<input>` tag for the "Name" and update it like this
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								+makeExample('forms/ts/app/hero-form.component.html', 'ngModel-1','app/hero-form.component.html (excerpt)')(format=".")
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								.l-sub-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  :marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    We appended a diagnostic interpolation after the input tag
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    so we can see what we're doing.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    We left ourselves a note to throw it way when we're done.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Focus on the binding syntax: `[(ngModel)]="..."`.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  If we ran the app right now and started typing in the *Name* input box,
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  adding and deleting characters, we'd see them appearing and disappearing
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  from the interpolated text.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  At some point it might look like this.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								figure.image-display
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  img(src="/resources/images/devguide/forms/ng-model-in-action.png" width="400px" alt="ngModel in action")
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  The diagnostic is evidence that we really are flowing values from the input box to the model and
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  back again. **That's two-way data binding!**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Let's add similar `[(ngModel)]` bindings to *Alter Ego* and *Hero Power*.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  We'll ditch the input box binding message
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  and add a new binding at the top to the component's `diagnostic` property.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Then we can confirm that two-way data binding works *for the entire Hero model*.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  After revision the core of our form should have three `[(ngModel)]` bindings that
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  look much like this:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								+makeExample('forms/ts/app/hero-form.component.html', 'ngModel-2', 'app/hero-form.component.html (excerpt)')
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  If we ran the app right now and changed every Hero model property, the form might display like this:
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								figure.image-display
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  img(src="/resources/images/devguide/forms/ng-model-in-action-2.png" width="400px" alt="ngModel in super action")
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  The diagnostic near the top of the form
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  confirms that all of our changes are reflected in the model.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  **Delete** the `{{diagnostic}}` binding at the top as it has served its purpose.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								.l-sub-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  :marked
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    ### Inside [(ngModel)]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    *This section is an optional deep dive into [(ngModel)]. Not interested? Skip ahead!*
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    The punctuation in the binding syntax, <span style="font-family:courier"><b>[()]</b></span>, is a good clue to what's going on.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    In a Property Binding, a value flows from the model to a target property on screen.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    We identify that target property by surrounding its name in brackets, <span style="font-family:courier"><b>[]</b></span>.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    This is a one-way data binding **from the model to the view**.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    In an Event Binding, we flow the value from the target property on screen to the model.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    We identify that target property by surrounding its name in parentheses, <span style="font-family:courier"><b>()</b></span>.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    This is a one-way data binding in the opposite direction **from the view to the model**.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    No wonder Angular chose to combine the punctuation as <span style="font-family:courier"><b>[()]</b></span>
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    to signify a two-way data binding and a **flow of data in both directions**.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    In fact, we can break the `NgModel` binding into its two separate modes
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    as we do in this re-write of the "Name" `<input>` binding:
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  +makeExample('forms/ts/app/hero-form.component.html', 'ngModel-3','app/hero-form.component.html (excerpt)')(format=".")
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:30:15 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  :marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    <br>The Property Binding should feel familiar. The Event Binding might seem strange.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    The `ngModelChange` is not an `<input>` element event.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    It is actually an event property of the `NgModel` directive.
							 
						 
					
						
							
								
									
										
										
										
											2015-12-25 13:45:08 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    When Angular sees a binding target in the form <span style="font-family:courier">[(x)]</span>,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    it expects the `x` directive to have an `x` input property and an `xChange` output property.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    The other oddity is the template expression, `model.name = $event`.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    We're used to seeing an `$event` object coming from a DOM event.
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    The `ngModelChange` property doesn't produce a DOM event; it's an Angular `EventEmitter`
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    property that returns the input box value when it fires — which is precisely what
							 
						 
					
						
							
								
									
										
										
										
											2016-01-03 17:17:24 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    we should assign to the model's `name` property.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    Nice to know but is it practical? We almost always prefer `[(ngModel)]`.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    We might split the binding if we had to do something special in
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    the event handling such as debounce or throttle the key strokes.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    Learn more about `NgModel` and other template syntax in the
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    [Template Syntax](./template-syntax.html) chapter.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  ## Track change-state and validity with **ngControl**
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  A form isn't just about data binding. We'd also like to know the state of the controls on our form.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  The `NgControl` directive keeps track of control state for us.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:30:15 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								.callout.is-helpful
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  header NgControl requires Form
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  :marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    The `NgControl` is one of a family of `NgForm` directives that can only be applied to
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    a control within a `<form`> tag.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Our application can ask an `NgControl` if the user touched the control,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  if the value changed, or if the value became invalid.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  `NgControl` doesn't just track state; it updates the control with special
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Angular CSS classes from the set we listed above.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  We can leverage those class names to change the appearance of the
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  control and make messages appear or disappear.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  We'll explore those effects soon. Right now
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  we should **add `ngControl`to all three form controls**,
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  starting with the *Name* input box
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								+makeExample('forms/ts/app/hero-form.component.html', 'ngControl-1', 'app/hero-form.component.html (excerpt)')(format=".")
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Be sure to assign a unique name to each `ngControl` directive.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								.l-sub-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  :marked
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    Angular registers controls under their `ngControl` names
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    with the `NgForm`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    We didn't add the `NgForm` directive explicitly but it's here
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    and we'll talk about it [later in this chapter](#ngForm).
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  ## Add Custom CSS for Visual Feedback
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  `NgControl` doesn't just track state. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  It updates the control with three classes that reflect the state.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								table
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  tr
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    th State
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    th Class if true
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    th Class if false
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  tr
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    td Control has been visited
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    td <code>ng-touched</code>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    td <code>ng-untouched</code>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  tr
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    td Control's value has changed
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    td <code>ng-dirty</code>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    td <code>ng-pristine</code>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  tr
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    td Control's value is valid
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    td <code>ng-valid</code>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    td <code>ng-invalid</code>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Let's add a temporary [local template variable](./template-syntax.html#local-vars) named **spy**
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  to the "Name" `<input>` tag and use the spy to display those classes.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								+makeExample('forms/ts/app/hero-form.component.html', 'ngControl-2','app/hero-form.component.html (excerpt)')(format=".")
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Now run the app and focus on the *Name* input box.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  Follow the next four steps *precisely*
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  1. Look but don't touched
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  1. Click in the input box, then click outside the text input box
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  1. Add slashes to the end of the name
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  1. Erase the name
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  The actions and effects are as follows:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								figure.image-display
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  img(src="/resources/images/devguide/forms/control-state-transitions-anim.gif"  alt="Control State Transition")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  We should be able to see the following four sets of class names and their transitions:
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								figure.image-display
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  img(src="/resources/images/devguide/forms/ng-control-class-changes.png" width="400px" alt="Control State Transitions")
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  The (`ng-valid` | `ng-invalid`) pair are most interesting to us. We want to send a
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  strong visual signal when the data are invalid and we want to mark required fields.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  We realize we can do both at the same time with a colored bar on the left of the input box:
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								figure.image-display
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  img(src="/resources/images/devguide/forms/validity-required-indicator.png" width="400px" alt="Invalid Form")
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  We achieve this effect by adding two styles to a new `styles.css` file
							 
						 
					
						
							
								
									
										
										
										
											2015-11-07 20:54:31 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  that we add to our project as a sibling to `index.html`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								+makeExample('forms/ts/styles.css',null,'styles.css')(format=".")
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-11-07 20:54:31 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  These styles select for the two Angular validity classes and the HTML 5 "required" attribute.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-07 20:54:31 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  We update the `<head>` of the `index.html` to include this style sheet.
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								+makeExample('forms/ts/index.html', 'styles', 'index.html (excerpt)')(format=".")
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  ## Show and Hide Validation Error messages
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  We can do better.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  The "Name" input box is required. Clearing it turns the bar red. That says *something* is wrong but we
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  don't know *what* is wrong or what to do about it.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  We can leverage the `ng-invalid` class to reveal a helpful message.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Here's the way it should look when the user deletes the name:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								figure.image-display
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  img(src="/resources/images/devguide/forms/name-required-error.png" width="400px" alt="Name required")
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  To achieve this effect we extend the `<input>` tag with
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  1. a [local template variable](./template-syntax.html#local-vars)
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  1. the "*is required*" message in a nearby `<div>` which we'll display only if the control is invalid.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Here's how we do it for the *name* input box:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								-var stylePattern = { otl: /(#name="form")|(.*div.*$)|(Name is required)/gm };
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								+makeExample('forms/ts/app/hero-form.component.html', 
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  'name-with-error-msg', 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  'app/hero-form.component.html (excerpt)', 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  stylePattern)
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  When we added the `ngControl` directive, we bound it to the the model's `name` property.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  Here we initialize a template local variable (`name`) with the value "ngForm" (`#name="ngForm"`).
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  Angular recognizes that syntax and re-sets the `name` local template variable to the
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  `ngControl` directive instance.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  In other words, the `name` local template variable becomes a handle on the `ngControl` object
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  for this input box.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  Now we can control visibility of the "name" error message by binding the message `<div>` element's `hidden` property 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  to the `ngControl` object's `valid` property. The message is hidden while the control is valid;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  the message is revealed when the control becomes invalid.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								<a id="ngForm"></a>
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								.l-sub-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  :marked
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    ### The NgForm directive
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    We just set a template local variable with the value of an `NgForm` directive. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    Why did that work? We didn't add the **[`NgForm`](../api/core/NgForm-class.html) directive** explicitly.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    Angular added it surreptiously, wrapping it around the `<form>` element
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-12 21:28:54 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    The `NgForm` directive  supplements the `form` element with additional features.
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    It collects `Controls` (elements identified by an `ngControl` directive)
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    and monitors their properties including their validity.
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    It also has its own `valid` property which is true only if every contained
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    control is valid.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  The Hero *Alter Ego* is optional so we can leave that be.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Hero *Power* selection is required.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  We can add the same kind of error handling to the `<select>` if we  want
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  but it's not imperative because the selection box already constrains the
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  power to valid value.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  ## Submit the form with **ngSubmit**
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  The user should be able to submit this form after filling it in.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  The Submit button at the bottom of the form
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  does nothing on its own but it will
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  trigger a form submit because of its type (`type="submit"`).
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  A "form submit" is useless at the moment.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  To make it useful, we'll update the `<form>` tag with another Angular directive, `NgSubmit`,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  and bind it to the `HeroFormComponent.submit()` method with an  event binding
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								+makeExample('forms/ts/app/hero-form.component.html', 'ngSubmit')(format=".")
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  We slipped in something extra there at the end!  We defined a
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  template local variable, **`#heroForm`**, and initialized it with the value, "ngForm".
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  The variable `heroForm` is now a handle to the `NgForm` directive that we [discussed earlier](#ngForm)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  This time `heroForm` remains a reference to the form as a whole.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Later in the template we bind the button's `disabled` property to the form's over-all validity via
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  the `heroForm` variable. Here's that bit of markup:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								+makeExample('forms/ts/app/hero-form.component.html', 'submit-button')
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Re-run the application. The form opens in a valid state and the button is enabled.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Now delete the *Name*. We violate the "name required" rule which
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  is duely noted in our error message as before. And now the Submit button is also disabled.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Not impressed?  Think about it for a moment. What would we have to do to
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  wire the button's enable/disabled state to the form's validity without Angular's help?
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  For us, it was as simple as
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  1. Define a template local variable on the (enhanced) form element
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  2. Reference that variable in a button some 50 lines away.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  ## Toggle two form regions (extra credit)
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Submitting the form isn't terribly dramatic at the moment.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								.l-sub-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  :marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    An unsurprising observation for a demo. To be honest,
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    jazzing it up won't teach us anything new about forms.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    But this is an opportunity to exercise some of our newly won
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    binding skills.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    If you're not interested, you can skip to the chapter's conclusion
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    and not miss a thing.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  Let's do something more strikingly visual.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Let's hide the data entry area and display something else.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Start by wrapping the form in a `<div>` and bind
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  its `hidden` property to the `HeroFormComponent.submitted` property.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								+makeExample('forms/ts/app/hero-form.component.html', 'edit-div', 'app/hero-form.component.html (excerpt)')(format=".")
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  The main form is visible from the start because the
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  the `submitted` property is false until we submit the form,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  as this fragment from the `HeroFormComponent` reminds us:
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								+makeExample('forms/ts/app/hero-form.component.ts', 'submitted')(format=".")
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  When we click the Submit button, the `submitted` flag becomes true and the form disappears
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  as planned.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Now we need to show something else while the form is in the submitted state.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  Add the following block of HTML below the `<div>` wrapper we just wrote:
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								+makeExample('forms/ts/app/hero-form.component.html', 'submitted', 'app/hero-form.component.html (excerpt)')
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  There's our hero again, displayed read-only with interpolation bindings.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  This slug of HTML only appears while the component is in the submitted state.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  We added an Edit button whose click event is bound to an expression
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  that clears the `submitted` flag.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-30 20:41:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  When we click it, this block disappears and the editable form reappears.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  That's as much drama as we can muster for now.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  ## Conclusion
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  The Angular 2 form discussed in this chapter takes advantage of the following framework features to provide support for data modification, validation and more:
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  - An Angular HTML form template.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  - A form component class with a `Component` decorator.
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  - The `ngSubmit` directive for handling the form submission.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  - Template local variables such as `#heroForm`, `#name`, `#alter-ego` and `#power`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  - The `ngModel` directive for two-way data binding.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  - The `ngControl` for validation and form element change tracking.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-20 16:10:44 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  - The local variable’ 
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  - Controlling the submit button's enabled state by binding to `NgForm` validity.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  - Custom CSS classes that provide visual feedback to users about invalid controls.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Our final project folder structure should look like this:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								code-example(format="").
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  angular2-forms
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  ├── node_modules
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  ├── app
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  |    ├── app.component.ts
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  |    ├── boot.ts  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  |    ├── hero.ts
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  |    ├── hero-form.component.html
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  |    └── hero-form.component.ts
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  ├── index.html
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  ├── styles.css
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  ├── tsconfig.json
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  └── package.json
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								  Here’ 
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-07 20:54:31 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								+makeTabs(
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  `forms/ts/app/hero-form.component.ts,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								   forms/ts/app/hero-form.component.html,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								   forms/ts/app/hero.ts,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								   forms/ts/app/app.component.ts,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								   forms/ts/app/boot.ts,   
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								   forms/ts/index.html,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								   forms/ts/styles.css`,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  'final, final,,,,,',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  `hero-form.component.ts,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								   hero-form.component.html,   
							 
						 
					
						
							
								
									
										
										
										
											2015-11-07 20:54:31 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								   hero.ts,
							 
						 
					
						
							
								
									
										
										
										
											2015-12-11 11:50:57 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								   app.component.ts,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								   boot.ts,
							 
						 
					
						
							
								
									
										
										
										
											2015-11-07 20:54:31 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								   index.html,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								   styles.css`)
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:18:44 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked