@@ -105,53 +105,49 @@ figure.image-display
105
105
to implement a keystroke loopback in a simple template.
106
106
+ makeExample('user-input/ts/app/loop-back.component.ts' , 'loop-back-component' , 'app/loop-back.component.ts' )( format ="." )
107
107
:marked
108
- We've declared a template reference variable named `box` on the `<input>` element.
109
- The `box` variable is a reference to the `<input>` element itself, which means we can
110
- grab the input element's `value` and display it
108
+ The template reference variable named `box`, declared on the `<input>` element,
109
+ refers to the `<input>` element itself.
110
+ The code uses the `box` variable to get the input element's `value` and display it
111
111
with interpolation between `<p>` tags.
112
112
113
113
The template is completely self contained. It doesn't bind to the component,
114
114
and the component does nothing.
115
115
116
- Type in the input box, and watch the display update with each keystroke. *Voila!*
116
+ Type something in the input box, and watch the display update with each keystroke.
117
117
118
118
figure.image-display
119
119
img( src ='/resources/images/devguide/user-input/keyup-loop-back-anim.gif' alt ="loop back" )
120
120
.l-sub-section
121
121
:marked
122
- **This won't work at all unless we bind to an event**.
123
-
124
- Angular only updates the bindings (and therefore the screen)
125
- if we do something in response to asynchronous events such as keystrokes.
126
-
127
- That's why we bind the `keyup` event to a statement that does ... well, nothing.
128
- We're binding to the number 0, the shortest statement we can think of.
129
- That is all it takes to keep Angular happy. We said it would be clever!
130
- :marked
131
- That template reference variable is intriguing. It's clearly easier to get to the textbox with that
132
- variable than to go through the `$event` object. Maybe we can rewrite our previous
133
- keyup example so that it uses the variable to get the user's input. Let's give it a try .
122
+ **This won't work at all unless you bind to an event**.
123
+
124
+ Angular updates the bindings (and therefore the screen)
125
+ only if the app does something in response to asynchronous events, such as keystrokes.
126
+ This example code binds the `keyup` event
127
+ to the number 0, the shortest template statement possible. The statement
128
+ does nothing, but it satisfies Angular's requirement so that Angular will
129
+ update the screen.
130
+ :marked
131
+ It's easier to get to the textbox with the template reference
132
+ variable than to go through the `$event` object. Here's a rewrite of the previous
133
+ ` keyup` example that uses a template reference variable to get the user's input.
134
134
+ makeExample('user-input/ts/app/keyup.components.ts' , 'key-up-component-2' ,'app/keyup.components.ts (v2)' )( format ="." )
135
135
:marked
136
- That sure seems easier.
137
- An especially nice aspect of this approach is that our component code gets clean data values from the view.
136
+ A nice aspect of this approach is that the component code gets clean data values from the view.
138
137
It no longer requires knowledge of the `$event` and its structure.
139
138
140
139
<a id =" key-event" ></a >
141
140
.l-main-section
142
141
:marked
143
142
## Key event filtering (with `key.enter`)
144
- Perhaps we don't care about every keystroke.
145
- Maybe we're only interested in the input box value when the user presses Enter, and we'd like to ignore all other keys.
146
- When we bind to the `(keyup)` event, our event handling statement hears *every keystroke*.
147
- We could filter the keys first, examining every `$event.keyCode`, and update the `values` property only if the key is Enter.
148
-
149
- Angular can filter the key events for us. Angular has a special syntax for keyboard events.
150
- We can listen for just the Enter key by binding to Angular's `keyup.enter` pseudo-event.
151
-
152
- Only then do we update the component's `values` property. (In this example,
153
- the update happens inside the event binding statement. A better practice
154
- would be to put the update code in the component.)
143
+ Sometimes only the Enter key matters, because it signals that the user has finished typing.
144
+ But the `(keyup)` event handler hears *every keystroke.*
145
+ One way to reduce the noise would be to examine every `$event.keyCode` and take action when the key is Enter.
146
+ But there's an easier way: bind to Angular's `keyup.enter` pseudo-event. With this pseudo-event,
147
+ Angular calls the event handler only when the user presses Enter.
148
+
149
+ In this example, the data binding expression handles the event. It's a better practice
150
+ to minimize JavaScript in HTML. Move this code to the component.
155
151
+ makeExample('user-input/ts/app/keyup.components.ts' , 'key-up-component-3' ,'app/keyup.components.ts (v3)' )( format ="." )
156
152
:marked
157
153
Here's how it works.
@@ -162,42 +158,40 @@ figure.image-display
162
158
:marked
163
159
## On blur
164
160
165
- Our previous example won't transfer the current state of the input box if the user mouses away and clicks
166
- elsewhere on the page. We update the component's `values` property only when the user presses Enter
167
- while the focus is inside the input box.
161
+ In the previous example, the current state of the input box
162
+ is lost if the user mouses away and clicks elsewhere on the page
163
+ without first pressing Enter.
164
+ The component's `values` property is updated only when the user presses Enter.
168
165
169
- Let's fix that by listening to the input box's blur event as well .
166
+ To fix this issue, listen to both the Enter key and the blur event .
170
167
171
168
+ makeExample('user-input/ts/app/keyup.components.ts' , 'key-up-component-4' ,'app/keyup.components.ts (v4)' )( format ="." )
172
169
173
170
.l-main-section
174
171
:marked
175
172
## Put it all together
176
- We learned how to [display data](./displaying-data.html) in the previous chapter .
177
- We've acquired a small arsenal of event binding techniques in this chapter .
173
+ The previous page showed how to [display data](./displaying-data.html).
174
+ This page demonstrated event binding techniques.
178
175
179
- Let's put it all together in a micro-app
180
- that can display a list of heroes and add new heroes to that list.
181
- The user can add a hero by first typing in the input box and then
182
- pressing Enter, clicking the Add button, or clicking elsewhere on the page .
176
+ Now, put it all together in a micro-app
177
+ that can display a list of heroes and add new heroes to the list.
178
+ The user can add a hero by typing the hero's name in the input box and
179
+ clicking ** Add** .
183
180
184
181
figure.image-display
185
182
img( src ='/resources/images/devguide/user-input/little-tour-anim.gif' alt ="Little Tour of Heroes" )
186
183
:marked
187
184
Below is the "Little Tour of Heroes" component.
188
- We'll call out the highlights after we bask briefly in its minimalist glory.
189
185
190
186
+ makeExample('user-input/ts/app/little-tour.component.ts' , 'little-tour' , 'app/little-tour.component.ts' )( format ="." )
191
187
:marked
192
- We've seen almost everything here before. A few things are new or bear repeating.
193
-
194
188
### Use template variables to refer to elements
195
189
196
190
The `newHero` template variable refers to the `<input>` element.
197
- We can use `newHero` from any sibling or child of the `<input>` element.
191
+ You can use `newHero` from any sibling or child of the `<input>` element.
198
192
199
193
Getting the element from a template variable makes the button click handler
200
- simpler. Without the variable, we'd have to use a fancy CSS selector
194
+ simpler. Without the variable, the code would have to include a CSS selector
201
195
to find the input element.
202
196
203
197
### Pass values, not elements
0 commit comments