2016-02-05 23:27:06 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								include ../_util-fns
							 
						 
					
						
							
								
									
										
										
										
											2015-10-31 14:45:11 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  When the user clicks a link, pushes a button, or enters text
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 20:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  we want to know about it. These user actions all raise DOM events.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  In this chapter we learn to bind to those events using the Angular
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  event binding syntax.
							 
						 
					
						
							
								
									
										
										
										
											2015-12-16 23:11:50 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  [Run the live example](/resources/live-examples/user-input/ts/plnkr.html)
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 20:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  ## Binding to user input events
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 20:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We can use [Angular event bindings](./template-syntax.html#event-binding)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  to respond to [any DOM event](https://developer.mozilla.org/en-US/docs/Web/Events).
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-17 21:23:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The syntax is simple. We surround the DOM event name in parentheses and assign a quoted template statement to it.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  As an example, here's an event binding that implements a click handler:
							 
						 
					
						
							
								
									
										
										
										
											2015-12-13 15:15:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								+makeExample('user-input/ts/app/click-me.component.ts', 'click-me-button')(format=".", language="html")
							 
						 
					
						
							
								
									
										
										
										
											2015-10-31 14:45:11 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-28 20:43:09 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								<a id="click"></a>
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 20:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The `(click)` to the left of the equal sign identifies the button's click event as the **target of the binding**.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-17 21:23:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The text within quotes on the right is the **template statement** in which we
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  respond to the click event by calling the component's `onClickMe` method. A [template statement](./template-syntax.html#template-statements) is a subset
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  of JavaScript with restrictions and a few added tricks.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-17 21:23:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  When writing a binding we must be aware of a template statement's **execution context**.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-25 03:39:34 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The identifiers appearing within a statement belong to a specific context object.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 20:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  That object is usually the Angular component that controls the template  ... which it definitely is
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  in this case because that snippet of HTML belongs to the following component:
							 
						 
					
						
							
								
									
										
										
										
											2015-12-13 15:15:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								+makeExample('user-input/ts/app/click-me.component.ts', 'click-me-component', 'app/click-me.component.ts')(format=".")
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 20:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  When the user clicks the button, Angular calls the component's `onClickMe` method.
							 
						 
					
						
							
								
									
										
										
										
											2015-08-08 13:55:53 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 20:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  ## Get user input from the $event object
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We can bind to all kinds of events. Let's bind to the keyup event of an input box and replay
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 20:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  what the user types back onto the screen.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-13 15:15:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  This time we'll (1) listen to an event and (2) grab the user's input.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								+makeExample('user-input/ts/app/keyup.components.ts', 'key-up-component-1-template', 'app/keyup.components.ts (template v.1)')(format=".")
							 
						 
					
						
							
								
									
										
										
										
											2015-12-13 15:15:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Angular makes an event object available in the **`$event`** variable,
							 
						 
					
						
							
								
									
										
										
										
											2015-12-16 23:11:50 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  which we pass to the component's `onKey()` method.
							 
						 
					
						
							
								
									
										
										
										
											2015-12-13 15:15:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The user data we want is in that variable somewhere.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								+makeExample('user-input/ts/app/keyup.components.ts', 'key-up-component-1-class-no-type', 'app/keyup.components.ts (class v.1)')(format=".")
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 20:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The shape of the `$event` object is determined by whatever raises the event.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The `keyup` event comes from the DOM, so `$event` must be a [standard DOM event object](https://developer.mozilla.org/en-US/docs/Web/API/Event).
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  The `$event.target` gives us an
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  [`HTMLInputElement`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement), which
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  has a `value` property that contains our user input data.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The `onKey()` component method is where we extract the user's input
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  from the event object, adding that input to the list of user data that we're accumulating in the component's `values` property.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 20:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We then use [interpolation](./template-syntax.html#interpolation)
							 
						 
					
						
							
								
									
										
										
										
											2015-12-13 15:15:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  to display the accumulating `values` property back on screen.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Enter the letters "abc", and then backspace to remove them.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  Here's what the UI displays:
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 20:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								code-example().
							 
						 
					
						
							
								
									
										
										
										
											2015-10-31 23:54:11 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  a | ab | abc | ab | a | |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								figure.image-display
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    img(src='/resources/images/devguide/user-input/keyup1-anim.gif' alt="key up 1")
							 
						 
					
						
							
								
									
										
										
										
											2015-12-13 15:15:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								<a id="keyup1"></a>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								.l-sub-section
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  :marked
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    We cast the `$event` as an `any` type, which means we've abandoned strong typing
							 
						 
					
						
							
								
									
										
										
										
											2015-12-13 15:15:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    to simplify our code. We generally prefer the strong typing that TypeScript affords.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    We can rewrite the method, casting to HTML DOM objects like this.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  +makeExample('user-input/ts/app/keyup.components.ts', 'key-up-component-1-class', 'app/keyup.components.ts (class v.1 - strongly typed )')(format=".")
							 
						 
					
						
							
								
									
										
										
										
											2015-12-13 15:15:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  :marked
							 
						 
					
						
							
								
									
										
										
										
											2015-12-16 23:11:50 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    <br>Strong typing reveals a serious problem with passing a DOM event into the method:
							 
						 
					
						
							
								
									
										
										
										
											2015-12-13 15:15:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    too much awareness of template details, too little separation of concerns.
							 
						 
					
						
							
								
									
										
										
										
											2015-12-16 23:11:50 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-13 15:15:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    We'll address this problem in our next try at processing user keystrokes.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-08-08 13:55:53 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-04-28 11:18:52 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  ## Get user input from a template reference variable
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 20:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  There's another way to get the user data without the `$event` variable.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-04-28 11:18:52 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Angular has a syntax feature called [**template reference variables**](./template-syntax.html#ref-vars).
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 20:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  These variables grant us direct access to an element.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-28 11:18:52 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We declare a template reference variable by preceding an identifier with a hash/pound character (#).
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-04-28 11:18:52 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Here's an example of using a template reference variable
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  to implement a clever keystroke loopback in an ultra-simple template.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+makeExample('user-input/ts/app/loop-back.component.ts', 'loop-back-component', 'app/loop-back.component.ts')(format=".")
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-04-28 11:18:52 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We've declared a template reference variable named `box` on the `<input>` element.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The `box` variable is a reference to the `<input>` element itself, which means we can
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  grab the input element's `value` and display it
							 
						 
					
						
							
								
									
										
										
										
											2015-12-16 23:11:50 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  with interpolation between `<p>` tags.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The template is completely self contained. It doesn't bind to the component,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  and the component does nothing.
							 
						 
					
						
							
								
									
										
										
										
											2015-12-16 23:11:50 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Type in the input box, and watch the display update with each keystroke. *Voila!*
							 
						 
					
						
							
								
									
										
										
										
											2015-12-16 23:11:50 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-13 15:15:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								figure.image-display
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    img(src='/resources/images/devguide/user-input/keyup-loop-back-anim.gif' alt="loop back")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								.l-sub-section
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  :marked
							 
						 
					
						
							
								
									
										
										
										
											2015-12-16 23:11:50 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    **This won't work at all unless we bind to an event**.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-13 15:15:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    Angular only updates the bindings (and therefore the screen)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if we do something in response to asynchronous events such as keystrokes.
							 
						 
					
						
							
								
									
										
										
										
											2015-12-16 23:11:50 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-25 03:39:34 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    That's why we bind the `keyup` event to a statement that does ... well, nothing.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-17 21:23:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    We're binding to the number 0, the shortest statement we can think of.
							 
						 
					
						
							
								
									
										
										
										
											2015-12-13 15:15:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    That is all it takes to keep Angular happy. We said it would be clever!
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-04-28 11:18:52 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  That template reference variable is intriguing. It's clearly easier to get to the textbox with that
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  variable than to go through the `$event` object. Maybe we can rewrite our previous
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  keyup example so that it uses the variable to get the user's input. Let's give it a try.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+makeExample('user-input/ts/app/keyup.components.ts', 'key-up-component-2' ,'app/keyup.components.ts (v2)')(format=".")
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  That sure seems easier.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 20:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  An especially nice aspect of this approach is that our component code gets clean data values from the view.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  It no longer requires knowledge of the `$event` and its structure.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-31 14:45:11 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								<a id="key-event"></a>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-31 14:45:11 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  ## Key event filtering (with `key.enter`)
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Perhaps we don't care about every keystroke.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Maybe we're only interested in the input box value when the user presses Enter, and we'd like to ignore all other keys.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-17 21:23:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  When we bind to the `(keyup)` event, our event handling statement hears *every keystroke*.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We could filter the keys first, examining every `$event.keyCode`, and update the `values` property only if the key is Enter.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  Angular can filter the key events for us. Angular has a special syntax for keyboard events.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We can listen for just the Enter key by binding to Angular's `keyup.enter` pseudo-event.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Only then do we update the component's `values` property. (In this example,
							 
						 
					
						
							
								
									
										
										
										
											2016-01-17 21:23:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  the update happens inside the event binding statement. A better practice
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  would be to put the update code in the component.)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+makeExample('user-input/ts/app/keyup.components.ts', 'key-up-component-3' ,'app/keyup.components.ts (v3)')(format=".")
							 
						 
					
						
							
								
									
										
										
										
											2015-12-13 15:15:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  Here's how it works.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-31 23:54:11 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								figure.image-display
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    img(src='/resources/images/devguide/user-input/keyup3-anim.gif' alt="key up 3")
							 
						 
					
						
							
								
									
										
										
										
											2015-10-31 17:24:50 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-31 17:24:50 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  ## On blur
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-31 17:24:50 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Our previous example won't transfer the current state of the input box if the user mouses away and clicks
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  elsewhere on the page. We update the component's `values` property only when the user presses Enter
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  while the focus is inside the input box.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-31 17:24:50 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Let's fix that by listening to the input box's blur event as well.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								+makeExample('user-input/ts/app/keyup.components.ts', 'key-up-component-4' ,'app/keyup.components.ts (v4)')(format=".")
							 
						 
					
						
							
								
									
										
										
										
											2015-10-31 17:24:50 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-08-08 13:55:53 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 20:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  ## Put it all together
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  We learned how to [display data](./displaying-data.html) in the previous chapter.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We've acquired a small arsenal of event binding techniques in this chapter.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 20:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Let's put it all together in a micro-app
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  that can display a list of heroes and add new heroes to that list.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  The user can add a hero by first typing in the input box and then
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  pressing Enter, clicking the Add button, or clicking elsewhere on the page.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 20:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								figure.image-display
							 
						 
					
						
							
								
									
										
										
										
											2015-10-31 23:54:11 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    img(src='/resources/images/devguide/user-input/little-tour-anim.gif' alt="Little Tour of Heroes")
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-12-13 15:15:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Below is the "Little Tour of Heroes"  component.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 20:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We'll call out the highlights after we bask briefly in its minimalist glory.
							 
						 
					
						
							
								
									
										
										
										
											2015-12-13 15:15:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								+makeExample('user-input/ts/app/little-tour.component.ts', 'little-tour', 'app/little-tour.component.ts')(format=".")
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 20:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We've seen almost everything here before. A few things are new or bear repeating.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  ### Use template variables to refer to elements
							 
						 
					
						
							
								
									
										
										
										
											2015-12-16 23:11:50 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The `newHero` template variable refers to the `<input>` element.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  We can use `newHero` from any sibling or child of the `<input>` element.
							 
						 
					
						
							
								
									
										
										
										
											2015-12-16 23:11:50 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Getting the element from a template variable makes the button click handler
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  simpler. Without the variable, we'd have to use a fancy CSS selector
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  to find the input element.
							 
						 
					
						
							
								
									
										
										
										
											2015-12-16 23:11:50 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  ### Pass values, not elements
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  We could have passed the `newHero` into the component's `addHero` method.
							 
						 
					
						
							
								
									
										
										
										
											2015-12-16 23:11:50 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-13 15:15:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  But that would require `addHero` to pick its way through the `<input>` DOM element,
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  something we learned to dislike in our first try at a [keyup component](#keyup1).
							 
						 
					
						
							
								
									
										
										
										
											2015-12-16 23:11:50 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Instead, we grab the input box *value* and pass *that* to `addHero`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  The component knows nothing about HTML or the DOM, which is the way we like it.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-17 21:23:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  ### Keep template statements simple
							 
						 
					
						
							
								
									
										
										
										
											2015-12-16 23:11:50 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We bound `(blur)` to *two* JavaScript statements.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We like the first one, which calls `addHero`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  We do not like the second one, which assigns an empty string to the input box value.
							 
						 
					
						
							
								
									
										
										
										
											2015-12-16 23:11:50 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The second statement exists for a good reason. We have to clear the input box after adding the new hero to the list.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  The component has no way to do that itself because it has no access to the
							 
						 
					
						
							
								
									
										
										
										
											2015-12-13 15:15:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  input box (our design choice).
							 
						 
					
						
							
								
									
										
										
										
											2015-12-16 23:11:50 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Although the example *works*, we are rightly wary of JavaScript in HTML.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-17 21:23:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Template statements are powerful. We're supposed to use them responsibly.
							 
						 
					
						
							
								
									
										
										
										
											2015-12-13 15:15:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Complex JavaScript in HTML is irresponsible.
							 
						 
					
						
							
								
									
										
										
										
											2015-12-16 23:11:50 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-13 15:15:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Should we reconsider our reluctance to pass the input box into the component?
							 
						 
					
						
							
								
									
										
										
										
											2015-12-16 23:11:50 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  There should be a better third way. And there is, as we'll see when we learn about `NgModel` in the [Forms](forms.html) chapter.
							 
						 
					
						
							
								
									
										
										
										
											2015-12-13 15:15:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  ## Source code
							 
						 
					
						
							
								
									
										
										
										
											2015-12-16 23:11:50 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-13 15:15:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Here is all the code we talked about in this chapter.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+makeTabs(`
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  user-input/ts/app/click-me.component.ts,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  user-input/ts/app/keyup.components.ts,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  user-input/ts/app/loop-back.component.ts,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  user-input/ts/app/little-tour.component.ts
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  `,'',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  `click-me.component.ts,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  keyup.components.ts,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  loop-back.component.ts,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  little-tour.component.ts`)
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 20:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  ## Summary
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 20:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We've mastered the basic primitives for responding to user input and gestures.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  As powerful as these primitives are, they are a bit clumsy for handling
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  large amounts of user input. We're operating down at the low level of events when
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  we should be writing two-way bindings between data entry fields and model properties.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-07 14:48:15 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Angular has a two-way binding called `NgModel`, which we'll learn about
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 20:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  in the `Forms` chapter.