@@ -3,22 +3,21 @@ include ../../../../_includes/_util-fns
3
3
<!-- http://plnkr.co/edit/x9JYbC -->
4
4
5
5
:marked
6
- ## Displaying Component Properties
7
-
8
6
We typically display data in Angular by binding controls in an HTML template
9
- to properties of an Angular Component .
7
+ to properties of an Angular component .
10
8
11
9
In this chapter, we'll create a component with a list of heroes. Each hero has a name.
12
10
We'll display the list of hero names and
13
11
conditionally show a selected hero in a detail area below the list.
14
12
15
- [Live Example](/resources/live-examples/displaying-data/ts/plnkr.html)
16
-
17
- Our final UI looks like this:
13
+ The final UI looks like this:
18
14
19
15
figure.image-display
20
16
img( src ="/resources/images/devguide/displaying-data/final.png" alt ="Final UI" )
21
17
18
+ :marked
19
+ [Run the live example](/resources/live-examples/displaying-data/ts/plnkr.html)
20
+
22
21
<a id =" interpolation" ></a >
23
22
.l-main-section
24
23
:marked
@@ -39,25 +38,25 @@ figure.image-display
39
38
:marked
40
39
We added two properties to the formerly empty component: `title` and `myHero`.
41
40
42
- Our revised template displays the two component properties using the double curly brace
41
+ Our revised template displays the two component properties using double curly brace
43
42
interpolation:
44
43
45
44
+ makeExample('displaying-data/ts/app/app.component.1.ts' , 'template' )( format ="." )
46
45
.l-sub-section
47
46
:marked
48
- The template is a multi-line string within ECMAScript 2015 back-tics (\`).
49
- The back-tick (\`) is not the same character as a single quote (').
50
- It has many nice features. The feature we're exploiting is
51
- the ability to compose the string over several lines which
52
- makes for much more readable HTML.
47
+ The template is a multi-line string within ECMAScript 2015 backticks (\`).
48
+ The backtick (\`) — which is * not* the same character as a single
49
+ quote (') — has many nice features. The feature we're exploiting here
50
+ is the ability to compose the string over several lines, which makes for
51
+ much more readable HTML.
53
52
54
53
:marked
55
54
Angular automatically pulls the value of the `title` and `myHero` properties from the component and
56
- inserts those values into the browser. Angular will update the display
55
+ inserts those values into the browser. Angular updates the display
57
56
when these properties change.
58
57
.l-sub-section
59
58
:marked
60
- More precisely, the re-display occurs after some kind of asynchronous event related to
59
+ More precisely, the redisplay occurs after some kind of asynchronous event related to
61
60
the view such as a keystroke, a timer completion, or an async `XHR` response.
62
61
We don't have those in this sample.
63
62
But then the properties aren't changing on their own either. For the moment we must operate on faith.
@@ -88,12 +87,14 @@ figure.image-display
88
87
## Template inline or template file?
89
88
90
89
We can store our component's template in one of two places.
91
- We can define it " inline" using the template property as we do here.
90
+ We can define it * inline* using the ` template` property, as we do here.
92
91
Or we can define the template in a separate HTML file and link to it in
93
92
the component metadata using the `@Component` decorator's `templateUrl` property.
94
93
95
- We're using the *inline* style because the template is small and it makes for clearer demonstration.
96
- The choice between them is a matter of taste, circumstances, and organization policy.
94
+ The choice between inline and separate HTML is a matter of taste,
95
+ circumstances, and organization policy.
96
+ Here we're using inline HTML because the template is small, and the demo
97
+ is simpler without the HTML file.
97
98
98
99
In either style, the template data bindings have the same access to the component's properties.
99
100
@@ -106,7 +107,7 @@ figure.image-display
106
107
+ makeExample('displaying-data/ts/app/app-ctor.component.ts' , 'app-ctor' )( format ="." )
107
108
108
109
:marked
109
- That's fine too. The choice between them is a matter of taste and organization policy.
110
+ That's fine too. The choice is a matter of taste and organization policy.
110
111
We'll adopt the more terse "variable assignment" style in this chapter simply because
111
112
there will be less code to read.
112
113
@@ -116,11 +117,11 @@ figure.image-display
116
117
## Showing an array property with NgFor
117
118
118
119
We want to display a list of heroes. We begin by adding a mock heroes name array to the component,
119
- just above `myHero` and redefine `myHero` to be the first name in the array.
120
+ just above `myHero`, and redefine `myHero` to be the first name in the array.
120
121
+ makeExample('displaying-data/ts/app/app.component.2.ts' , 'mock-heroes' , 'app/app.component.ts (class)' )( format ="." )
121
122
122
123
:marked
123
- Now we use the Angular `NgFor` "repeater" Directive in the template to display
124
+ Now we use the Angular `NgFor` "repeater" directive in the template to display
124
125
each item in the `heroes` list.
125
126
126
127
+ makeExample('displaying-data/ts/app/app.component.2.ts' , 'template' ,'app/app.component.ts (template)' )( format ="." )
@@ -132,19 +133,19 @@ figure.image-display
132
133
:marked
133
134
We added a somewhat mysterious `*ngFor` to the `<li>` element.
134
135
That's the Angular "repeater" directive.
135
- It's presence on the `<li>` tag marks that `<li>` element (and its children) as the "repeater template".
136
+ Its presence on the `<li>` tag marks that `<li>` element (and its children) as the "repeater template".
136
137
137
138
.alert.is-important
138
139
:marked
139
- Don't forget the leading asterisk (\*) in front of `*ngFor`. It is an essential part of the syntax.
140
+ Don't forget the leading asterisk (\*) in `*ngFor`. It is an essential part of the syntax.
140
141
Learn more about this and `NgFor` in the [Template Syntax](./template-syntax.html#ngFor) chapter.
141
142
142
143
:marked
143
144
Notice the `#hero` in the `NgFor` double-quoted instruction.
144
- The `#hero` is a "[template local variable](./template-syntax.html#local-vars")" * declaration* .
145
- The (#) prefix declares a local variable name named `hero`.
145
+ The `#hero` is a [ local template variable](./template-syntax.html#local-vars) declaration.
146
+ The `#` prefix declares a local variable name named `hero`.
146
147
147
- Angular will duplicate the `<li>` for each item in the list, setting the `hero` variable
148
+ Angular duplicates the `<li>` for each item in the list, setting the `hero` variable
148
149
to the item (the hero) in the current iteration. Angular uses that variable as the
149
150
context for the interpolation in the double curly braces.
150
151
@@ -168,67 +169,67 @@ figure.image-display
168
169
That's fine for a demo but certainly isn't a best practice. It's not even a good practice.
169
170
Although we won't do anything about that in this chapter, we'll make a mental note to fix this down the road.
170
171
171
- At the moment, we're binding to an array of strings. We do that occasionally in real applications but
172
- most of the time we're displaying objects, potentially instances of classes.
172
+ At the moment, we're binding to an array of strings. We do that occasionally in real applications, but
173
+ most of the time we're displaying objects — potentially instances of classes.
173
174
174
- Let's turn our array of hero names into an array of `Hero` objects. For that we'll need a `Hero' class.
175
+ Let's turn our array of hero names into an array of `Hero` objects. For that we'll need a `Hero` class.
175
176
176
177
Create a new file in the `app/` folder called `hero.ts` with the following short bit of code.
177
178
+ makeExample('displaying-data/ts/app/hero.ts' , null , 'app/hero.ts' )( format = "." )
178
179
179
180
:marked
180
181
We've defined a class with a constructor and two properties: `id` and `name`.
181
182
182
- If we are new to TypeScript, it may not look like we have properties. But we do. We're taking
183
- advantage of a TypeScript short-cut in our declaration of the constructor parameters.
183
+ It might not look like we have properties, but we do. We're taking
184
+ advantage of a TypeScript shortcut in our declaration of the constructor parameters.
184
185
185
186
Consider the first parameter:
186
187
+ makeExample('displaying-data/ts/app/hero.ts' , 'id-parameter' )
187
188
188
189
:marked
189
- That brief syntax simultaneously
190
+ That brief syntax does a lot:
190
191
* declares a constructor parameter and its type
191
- * declare a public property of the same name
192
- * initializes that property with the corresponding argument when we "new" an instance of the class.
192
+ * declares a public property of the same name
193
+ * initializes that property with the corresponding argument when we "new" an instance of the class
193
194
194
195
.l-main-section
195
196
:marked
196
- ## Use the Hero class
197
- Let's redefine the heroes property in our component to return an array of these Heroes
197
+ ## Using the Hero class
198
+ Let's redefine the ` heroes` property in our component to return an array of these Hero objects
198
199
and also set the `myHero` property with the first of these mock heroes.
199
200
+ makeExample('displaying-data/ts/app/app.component.3.ts' , 'heroes' , 'app.component.ts (excerpt)' )( format ="." )
200
201
201
202
:marked
202
203
We'll have to update the template.
203
- At the moment it displays the entire hero object which used to be a string value.
204
- Let's fix that so we interpolate the `hero.name` property
204
+ At the moment it displays the entire ` hero` object, which used to be a string value.
205
+ Let's fix that so we interpolate the `hero.name` property.
205
206
+ makeExample('displaying-data/ts/app/app.component.3.ts' , 'template' ,'app.component.ts (template)' )( format ="." )
206
207
207
208
:marked
208
- Our display looks the same but we know how much better it is under the hood .
209
+ Our display looks the same, but now we know much better what a hero really is .
209
210
210
211
<a id =" ngIf" ></a >
211
212
.l-main-section
212
213
:marked
213
214
## Conditional display with NgIf
214
215
215
- Sometimes the app should display a view or a portion of a view only under prescribed circumstances.
216
+ Sometimes the app should display a view or a portion of a view only under specific circumstances.
216
217
217
- In our example, we'd like to display a message if we have a large number of heroes ... say more than 3.
218
+ In our example, we'd like to display a message if we have a large number of heroes — say, more than 3.
218
219
219
- The Angular `NgIf` directive will insert or remove an element based on a truthy/falsey condition.
220
+ The Angular `NgIf` directive inserts or removes an element based on a truthy/falsey condition.
220
221
We can see it in action by adding the following paragraph at the bottom of the template:
221
222
+ makeExample('displaying-data/ts/app/app.component.ts' , 'message' )
222
223
.alert.is-important
223
224
:marked
224
- Don't forget the leading asterisk (\*) in front of `*ngIf`. It is an essential part of the syntax.
225
+ Don't forget the leading asterisk (\*) in `*ngIf`. It is an essential part of the syntax.
225
226
Learn more about this and `NgIf` in the [Template Syntax](./template-syntax.html#ngIf) chapter.
226
227
227
228
:marked
228
229
The [template expression](./template-syntax.html#template-expressions) inside the double quotes
229
- looks much like JavaScript and it is much like JavaScript.
230
+ looks much like JavaScript and it _is_ much like JavaScript.
230
231
When the component's list of heroes has more than 3 items, Angular adds the paragraph to the DOM and the message appears.
231
- If there were 3 or fewer items, Angular omits the paragraph and there is no message.
232
+ If there are 3 or fewer items, Angular omits the paragraph, so no message appears .
232
233
233
234
.alert.is-helpful
234
235
:marked
@@ -237,31 +238,23 @@ figure.image-display
237
238
we were conditionally including or excluding a big chunk of HTML with many data bindings.
238
239
239
240
:marked
240
- Try it out. We have four items in the array so the message should appear.
241
+ Try it out. Because the array has four items, the message should appear.
241
242
Go back into `app.component.ts` and delete or comment out one of the elements from the hero array.
242
243
The browser should refresh automatically and the message should disappear.
243
244
244
- Play with it.
245
-
246
245
.l-main-section
247
246
:marked
248
247
## Summary
249
- Now we know how to
250
- - use **interpolation** with the double curly braces to display a component property,
251
- - use **`NgFor`** to display a list of items,
252
- - use a TypeScript class to shape the model data for our component and display properties of that model,
253
- - use **`NgIf`** to conditionally display a chunk of HTML based on a boolean expression.
248
+ Now we know how to use:
249
+ - **interpolation** with double curly braces to display a component property
250
+ - **`NgFor`** to display a list of items
251
+ - a TypeScript class to shape the ** model data** for our component and display properties of that model
252
+ - **`NgIf`** to conditionally display a chunk of HTML based on a boolean expression
254
253
255
- Our final code:
254
+ Here's our final code:
256
255
257
256
+ makeTabs(` displaying-data/ts/app/app.component.ts,
258
257
displaying-data/ts/app/hero.ts,
259
258
displaying-data/ts/app/boot.ts` ,
260
259
'final,,' ,
261
260
'app/app.component.ts, app/hero.ts, boot.ts' )
262
-
263
- .l-main-section
264
- :marked
265
- ## Next Steps
266
- In addition to displaying data, most applications need to respond to user input.
267
- Learn about that in the [User Input](./user-input.html) chapter.
0 commit comments