@@ -6,31 +6,29 @@ include ../../../../_includes/_util-fns
6
6
In this chapter we learn to bind to those events using the Angular
7
7
event binding syntax.
8
8
9
- [Run the live example](/resources/live-examples/user-input/ts/plnkr.html)
10
-
11
9
:marked
12
10
## Binding to user input events
13
11
14
- We can use [ Angular event bindings]( ./template-syntax.html#event-binding)
12
+ We can use Angular event bindings <!-- PENDING: link to ./template-syntax.html#event-binding -->
15
13
to respond to [any DOM event](https://developer.mozilla.org/en-US/docs/Web/Events).
16
14
17
15
The syntax is simple. We assign a template expression to the DOM event name, surrounded in parentheses.
18
16
As an example, here's an event binding that implements a click handler:
19
- + makeExample('user-input/ts/app/click-me.component.ts ' , 'click-me-button' )( format ="." , language ="html" )
17
+ + makeExample('user-input/dart/lib/click_me_component.dart ' , 'click-me-button' )( format ="." , language ="html" )
20
18
21
19
<a id =" click" ></a >
22
20
:marked
23
21
The `(click)` to the left of the equal sign identifies the button's click event as the **target of the binding**.
24
22
The text within quotes on the right is the **template expression** in which we
25
- respond to the click event by calling the component's `onClickMe` method. A [ template expression]( ./template-syntax.html#template-expressions) is a subset
26
- of JavaScript with a few added tricks.
23
+ respond to the click event by calling the component's `onClickMe` method. A template expression <!-- PENDING: link to ./template-syntax.html#template-expressions --> is a subset
24
+ of Dart with a few added tricks.
27
25
28
26
When writing a binding we must be aware of a template expression's **execution context**.
29
27
The identifiers appearing within an expression belong to a specific context object.
30
28
That object is usually the Angular component that controls the template ... which it definitely is
31
29
in this case because that snippet of HTML belongs to the following component:
32
30
33
- + makeExample('user-input/ts/app/click-me.component.ts ' , 'click-me-component' , 'app/click-me.component.ts ' )( format ="." )
31
+ + makeExample('user-input/dart/lib/click_me_component.dart ' , 'click-me-component' , 'web/click_me_component.dart ' )( format ="." )
34
32
:marked
35
33
When the user clicks the button, Angular calls the component's `onClickMe` method.
36
34
@@ -41,12 +39,22 @@ include ../../../../_includes/_util-fns
41
39
what the user types back onto the screen.
42
40
43
41
This time we'll (1) listen to an event and (2) grab the user's input.
44
- + makeExample('user-input/ts/app/keyup.components.ts' , 'key-up-component-1-template' , 'app/keyup.components.ts (template v.1)' )( format ="." )
42
+ + makeExample('user-input/dart/lib/keyup_components.dart' , 'key-up-component-1-template' , 'web/keyup_components.dart (template v.1)' )( format ="." )
43
+
45
44
:marked
46
45
Angular makes an event object available in the **`$event`** variable,
47
46
which we pass to the component's `onKey()` method.
48
47
The user data we want is in that variable somewhere.
49
- + makeExample('user-input/ts/app/keyup.components.ts' , 'key-up-component-1-class-no-type' , 'app/keyup.components.ts (class v.1)' )( format ="." )
48
+
49
+ important
50
+ .callout.is-important
51
+ header $event vs. \$event
52
+ :marked
53
+ Templates in Dart files need a `\` in front of the `$`.
54
+ If the template is in an HTML file, use `$event` instead of `\$event`.
55
+
56
+ + makeExample('user-input/dart/lib/keyup_components.dart' , 'key-up-component-1-class-no-type' , 'web/keyup_components.dart (class v.1)' )( format ="." )
57
+
50
58
:marked
51
59
The shape of the `$event` object is determined by whatever raises the event.
52
60
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).
@@ -56,7 +64,7 @@ include ../../../../_includes/_util-fns
56
64
57
65
The `onKey()` component method is where we extract the user's input
58
66
from the event object, adding that input to the list of user data that we're accumulating in the component's `values` property.
59
- We then use [ interpolation]( ./template-syntax.html#interpolation)
67
+ We then use interpolation <!-- PENDING: link to ./template-syntax.html#interpolation) -->
60
68
to display the accumulating `values` property back on screen.
61
69
62
70
Enter the letters "abc", and then backspace to remove them.
@@ -70,9 +78,9 @@ figure.image-display
70
78
.l-sub-section
71
79
:marked
72
80
We cast the `$event` as an `any` type, which means we've abandoned strong typing
73
- to simplify our code. We generally prefer the strong typing that TypeScript affords.
81
+ to simplify our code. We generally prefer the strong typing that Dart affords.
74
82
We can rewrite the method, casting to HTML DOM objects like this.
75
- + makeExample('user-input/ts/app/keyup.components.ts ' , 'key-up-component-1-class' , 'app/keyup.components.ts (class v.1 - strongly typed )' )( format ="." )
83
+ + makeExample('user-input/dart/lib/keyup_components.dart ' , 'key-up-component-1-class' , 'web/keyup_components.dart (class v.1 - strongly typed )' )( format ="." )
76
84
:marked
77
85
<br>Strong typing reveals a serious problem with passing a DOM event into the method:
78
86
too much awareness of template details, too little separation of concerns.
@@ -85,13 +93,14 @@ figure.image-display
85
93
## Get user input from a local template variable
86
94
There's another way to get the user data without the `$event` variable.
87
95
88
- Angular has a syntax feature called [**local template variables**](./template-syntax.html#local-vars).
96
+ Angular has a syntax feature called **local template variables**.
97
+ <!-- PENDING: link to ./template-syntax.html#local-vars) -->
89
98
These variables grant us direct access to an element.
90
99
We declare a local template variable by preceding an identifier with a hash/pound character (#).
91
100
92
101
Here's an example of using a local template variable
93
102
to implement a clever keystroke loopback in an ultra-simple template.
94
- + makeExample('user-input/ts/app/loop-back.component.ts ' , 'loop-back-component' , 'app/loop-back.component.ts ' )( format ="." )
103
+ + makeExample('user-input/dart/lib/loop_back_component.dart ' , 'loop-back-component' , 'web/loop_back_component.dart ' )( format ="." )
95
104
:marked
96
105
We've declared a template local variable named `box` on the `<input>` element.
97
106
The `box` variable is a reference to the `<input>` element itself, which means we can
@@ -119,7 +128,7 @@ figure.image-display
119
128
That local template variable is intriguing. It's clearly easier to get to the textbox with that
120
129
variable than to go through the `$event` object. Maybe we can rewrite our previous
121
130
keyup example so that it uses the variable to get the user's input. Let's give it a try.
122
- + makeExample('user-input/ts/app/keyup.components.ts ' , 'key-up-component-2' ,'app/keyup.components.ts (v2)' )( format ="." )
131
+ + makeExample('user-input/dart/lib/keyup_components.dart ' , 'key-up-component-2' ,'web/keyup_components.dart (v2)' )( format ="." )
123
132
:marked
124
133
That sure seems easier.
125
134
An especially nice aspect of this approach is that our component code gets clean data values from the view.
@@ -140,7 +149,7 @@ figure.image-display
140
149
Only then do we update the component's `values` property. (In this example,
141
150
the update happens inside the event expression. A better practice
142
151
would be to put the update code in the component.)
143
- + makeExample('user-input/ts/app/keyup.components.ts ' , 'key-up-component-3' ,'app/keyup.components.ts (v3)' )( format ="." )
152
+ + makeExample('user-input/dart/lib/keyup_components.dart ' , 'key-up-component-3' ,'web/keyup_components.dart (v3)' )( format ="." )
144
153
:marked
145
154
Here's how it works.
146
155
figure.image-display
@@ -156,7 +165,7 @@ figure.image-display
156
165
157
166
Let's fix that by listening to the input box's blur event as well.
158
167
159
- + makeExample('user-input/ts/app/keyup.components.ts ' , 'key-up-component-4' ,'app/keyup.components.ts (v4)' )( format ="." )
168
+ + makeExample('user-input/dart/lib/keyup_components.dart ' , 'key-up-component-4' ,'web/keyup_components.dart (v4)' )( format ="." )
160
169
161
170
.l-main-section
162
171
:marked
@@ -175,7 +184,7 @@ figure.image-display
175
184
Below is the "Little Tour of Heroes" component.
176
185
We'll call out the highlights after we bask briefly in its minimalist glory.
177
186
178
- + makeExample('user-input/ts/app/little-tour.component.ts ' , 'little-tour' , 'app/little-tour.component.ts ' )( format ="." )
187
+ + makeExample('user-input/dart/lib/little_tour_component.dart ' , 'little-tour' , 'web/little_tour_component.dart ' )( format ="." )
179
188
:marked
180
189
We've seen almost everything here before. A few things are new or bear repeating.
181
190
@@ -199,7 +208,7 @@ figure.image-display
199
208
The component knows nothing about HTML or the DOM, which is the way we like it.
200
209
201
210
### Keep template expressions simple
202
- We bound `(blur)` to *two* JavaScript statements.
211
+ We bound `(blur)` to *two* Dart statements.
203
212
204
213
We like the first one, which calls `addHero`.
205
214
We do not like the second one, which assigns an empty string to the input box value.
@@ -208,9 +217,9 @@ figure.image-display
208
217
The component has no way to do that itself because it has no access to the
209
218
input box (our design choice).
210
219
211
- Although the example *works*, we are rightly wary of JavaScript in HTML.
220
+ Although the example *works*, we are rightly wary of Dart in HTML.
212
221
Template expressions are powerful. We're supposed to use them responsibly.
213
- Complex JavaScript in HTML is irresponsible.
222
+ Complex Dart in HTML is irresponsible.
214
223
215
224
Should we reconsider our reluctance to pass the input box into the component?
216
225
@@ -221,15 +230,15 @@ figure.image-display
221
230
222
231
Here is all the code we talked about in this chapter.
223
232
+ makeTabs(`
224
- user-input/ts/app/click-me.component.ts ,
225
- user-input/ts/app/keyup.components.ts ,
226
- user-input/ts/app/loop-back.component.ts ,
227
- user-input/ts/app/little-tour.component.ts
233
+ user-input/dart/lib/click_me_component.dart ,
234
+ user-input/dart/lib/keyup_components.dart ,
235
+ user-input/dart/lib/loop_back_component.dart ,
236
+ user-input/dart/lib/little_tour_component.dart
228
237
` ,'' ,
229
- ` click-me.component.ts ,
230
- keyup.components.ts ,
231
- loop-back.component.ts ,
232
- little-tour.component.ts ` )
238
+ ` click_me_component.dart ,
239
+ keyup_components.dart ,
240
+ loop_back_component.dart ,
241
+ little_tour_component.dart ` )
233
242
234
243
.l-main-section
235
244
:marked
0 commit comments