@@ -27,6 +27,15 @@ style.
27
27
28
28
Try the <live-example></live-example>.
29
29
30
+ .alert.is-helpful
31
+ :marked
32
+ `NgFor` and `ngFor` appear throughout the Angular documentation
33
+ and you may wonder what the difference is. The _directive_ name is `NgFor`.
34
+ Its _usage in a template_ is `ngFor`. This is critical in
35
+ differentiating between the *context* of the directive or
36
+ a *property* of the directive. `NgFor` has a `NgForTrackBy`
37
+ property. The attribute `ngFor` doesn't have any properties.
38
+
30
39
a#definition
31
40
.l-main-section
32
41
:marked
@@ -49,9 +58,10 @@ a#definition
49
58
You'll learn in this guide that the [asterisk (\*) is a convenience syntax](#asterisk). Angular "de-sugars" it into a `<template>` around the
50
59
host element and its descendents. Each structural directive does something different with that template.
51
60
52
- Three of the common, built-in structural directives —
53
- [NgIf](template-syntax.html#ngIf), [NgFor](template-syntax.html#ngFor), and [NgSwitch...](template-syntax.html#ngSwitch) —
54
- are described in the [_Template Syntax_](template-syntax.html) guide and seen in samples throughout the Angular documentation.
61
+ Three of the common, built-in structural directives—[NgIf](template-syntax.html#ngIf),
62
+ [NgFor](template-syntax.html#ngFor), and [NgSwitch...](template-syntax.html#ngSwitch)—are
63
+ described in the [_Template Syntax_](template-syntax.html) guide and seen in samples throughout the Angular documentation.
64
+ Here's an example of them in a template:
55
65
56
66
+ makeExample('structural-directives/ts/app/app.component.html' , 'built-in' )( format ="." )
57
67
:marked
84
94
## NgIf Case Study
85
95
86
96
`NgIf` is the simplest structural directive and the easiest to understand.
87
- It takes a boolean value and makes an entire chunk of DOM appear or disappear.
97
+ It takes a boolean value and makes an entire chunk of the DOM appear or disappear.
88
98
89
99
<<<<<<< dc4da634c40560e4eda55814676bc96a230428dc
90
100
+ makeExample('structural-directives/ts/src/app/structural-directives.component.html' , 'ngIf' )( format ="." )
@@ -103,9 +113,9 @@ figure.image-display
103
113
The top paragraph is in the DOM. The bottom, disused paragraph is not;
104
114
in its place is a comment about "template bindings" (more about that [later](#asterisk)).
105
115
106
- When the condition is false, `NgIf` removes its host element from DOM,
107
- detaches it from DOM events (the attachments that it made)
108
- detaches the component from Angular change detection and destroys it.
116
+ When the condition is false, `NgIf` removes its host element from the DOM,
117
+ detaches it from DOM events (the attachments that it made),
118
+ detaches the component from Angular change detection, and destroys it.
109
119
The component and DOM nodes can be garbage-collected and free up memory.
110
120
111
121
### Why *remove* rather than *hide*?
@@ -126,20 +136,20 @@ figure.image-display
126
136
Angular keeps checking for changes that could affect data bindings.
127
137
Whatever the component was doing, it keeps doing.
128
138
129
- Although invisible, the component — and all of its descendant components — tie up resources.
139
+ Although invisible, the component—and all of its descendant components—tie up resources.
130
140
The performance and memory burden can be substantial, responsiveness can degrade, and the user sees nothing.
131
141
132
142
On the positive side, showing the element again is quick.
133
143
The component's previous state is preserved and ready to display.
134
- The component doesn't re-initialize — an operation that could be expensive.
144
+ The component doesn't re-initialize—an operation that could be expensive.
135
145
So hiding and showing is sometimes the right thing to do.
136
146
137
147
But in the absence of a compelling reason to keep them around,
138
148
your preference should be to remove DOM elements that the user can't see
139
149
and recover the unused resources with a structural directive like `NgIf` .
140
150
141
151
**These same considerations apply to every structural directive, whether built-in or custom.**
142
- We should ask ourselves — and the users of our directives — to think carefully
152
+ We should ask ourselves—and the users of our directives—to think carefully
143
153
about the consequences of adding and removing elements and of creating and destroying components.
144
154
145
155
a#ngcontainer
@@ -168,16 +178,16 @@ a#ng-container
168
178
+ makeExample('structural-directives/ts/app/app.component.html' , 'ngif' )( format ="." )
169
179
170
180
:marked
171
- Introducing another container element — typically a `<span>` or `<div>` —
172
- to group the elements under a single _root_ is usually harmless.
181
+ Introducing another container element—typically a `<span>` or `<div>`—to
182
+ group the elements under a single _root_ is usually harmless.
173
183
_Usually_ ... but not _always_.
174
184
175
185
The grouping element may break the template appearance because CSS styles
176
186
neither expect nor accommodate the new layout.
177
187
For example, suppose you have the following paragraph layout.
178
188
+ makeExample('structural-directives/ts/app/app.component.html' , 'ngif-span' )( format ="." )
179
189
:marked
180
- You also have a CSS style rule that happens to apply to a `<span>` within `<p>`aragraph.
190
+ You also have a CSS style rule that happens to apply to a `<span>` within a `<p>`aragraph.
181
191
+ makeExample('structural-directives/ts/app/app.component.css' , 'p-span' )( format ="." )
182
192
:marked
183
193
The constructed paragraph renders strangely.
@@ -286,7 +296,7 @@ a#ngfor
286
296
Angular transforms the `*ngFor` in similar fashion from asterisk (\*) syntax through
287
297
template _attribute_ to template _element_.
288
298
289
- Here's a full-featured `ngFor`, written all three ways
299
+ Here's a full-featured `ngFor`, written all three ways:
290
300
+ makeExample('structural-directives/ts/app/app.component.html' , 'inside-ngfor' )( format ="." )
291
301
292
302
<<<<<<< dc4da634c40560e4eda55814676bc96a230428dc
@@ -296,7 +306,7 @@ a#ngfor
296
306
>>>>>>> docs(template-syntax/structural-directives): refresh both guides for style, accuracy, understanding
297
307
:marked
298
308
This is manifestly more complicated than `ngIf` and rightly so.
299
- `NgFor ` directive has more features, both required and optional, than the `NgIf` shown in this guide.
309
+ The `ngFor ` directive has more features, both required and optional, than the `NgIf` shown in this guide.
300
310
At minimum `NgFor` needs a looping variable (`let hero`) and a list (`heroes`).
301
311
302
312
You enable these features in the string assigned to `ngFor`, which you write in Angular's [microsyntax](microsyntax).
@@ -326,6 +336,14 @@ a#microsyntax
326
336
The `NgFor` directive sets its _context's_ `$implicit` property to the current item in the list during each iteration.
327
337
That becomes the current value of the `hero` variable in the example above.
328
338
339
+ .alert.is-critical
340
+ :marked
341
+ Ward: I got lost on this third point. ^^ By context, do we mean the element that the
342
+ directive is on, such as the `<div>`? Next, I was thrown by `$implicit`. What is that?
343
+ Obviously a property, but why the `$` and should I know more about properties of
344
+ contexts? Can we link to something for more info? I felt like this was touching
345
+ on deep stuff.
346
+ :marked
329
347
* The other _let_ variables (`i` and `odd`) are linked to one of the directive's _named context_ properties.
330
348
Look to the directive's documentation to see which properties are available and what they do.
331
349
`NgFor` has several, including `index` and `odd`.
@@ -385,13 +403,17 @@ a#ngswitch
385
403
:marked
386
404
## Inside the _NgSwitch_ directives
387
405
406
+ <<<<<<< a5705eb367d80adf19abf765d735b9d1189b52e6
388
407
<<<<<<< dc4da634c40560e4eda55814676bc96a230428dc
389
408
+ makeExample('structural-directives/ts/src/app/structural-directives.component.html' , 'asterisk' )( format ="." )
390
409
:marked
391
410
We're prefixing these directive names with an asterisk (\*).
392
411
= ======
393
412
The Angular _NgSwitch_ is actally a set of cooperating directives: `NgSwitch`, `NgSwitchCase`, and `NgSwitchDefault`.
394
413
>>>>>>> docs(template-syntax/structural-directives): refresh both guides for style, accuracy, understanding
414
+ = ======
415
+ The Angular _NgSwitch_ is actually a set of cooperating directives: `NgSwitch`, `NgSwitchCase`, and `NgSwitchDefault`.
416
+ >>>>>>> docs(template-syntax/structural-directives): Add edits and comments on structural-directives
395
417
396
418
Here's an example.
397
419
+ makeExample('structural-directives/ts/app/app.component.html' , 'ngswitch' )( format ="." )
@@ -409,6 +431,13 @@ a#ngswitch
409
431
`NgSwitchDefault` includes its host element when no `NgSwitchCase` does.
410
432
>>>>>>> docs(template-syntax/structural-directives): refresh both guides for style, accuracy, understanding
411
433
434
+ .alert.is-critical
435
+ :marked
436
+ Ward: I was following along until these last two sentences above ^^. What does
437
+ "includes its host element" mean? Possibly in the same way we mean when we say
438
+ that something is in an include?
439
+
440
+ :marked
412
441
As with other structural directives, the `NgSwitchCase` and `NgSwitchDefault`
413
442
can be "de-sugared" into the template _attribute_ form.
414
443
+ makeExample('structural-directives/ts/app/app.component.html' , 'ngswitch-template-attr' )( format ="." )
@@ -461,7 +490,7 @@ a#unless
461
490
## Write a structural directive
462
491
In this section, you write a `MyUnless` directive structural directive
463
492
that does the opposite of `NgIf`.
464
- `NgIf` displays the template content when the condition `true`.
493
+ `NgIf` displays the template content when the condition is `true`.
465
494
`MyUnless` displays the content when the condition is ***false***.
466
495
467
496
+ makeExample('structural-directives/ts/app/app.component.html' , 'myUnless-1' )( format ="." )
@@ -503,9 +532,18 @@ block unless-intro
503
532
Pick something short that fits you or your company.
504
533
In this example, the prefix is `my`.
505
534
535
+ <<<<<<< a5705eb367d80adf19abf765d735b9d1189b52e6
506
536
<<<<<<< dc4da634c40560e4eda55814676bc96a230428dc
507
537
+ makeExample('structural-directives/ts/src/app/unless.directive.ts' , 'unless-constructor' )( format ="." )
508
538
= ======
539
+ = ======
540
+ .alert.is-critical
541
+ :marked
542
+ Ward: Why is this called a *key* when it looks like the value in a
543
+ key value pair?^^
544
+
545
+ :marked
546
+ >>>>>>> docs(template-syntax/structural-directives): Add edits and comments on structural-directives
509
547
The directive _class_ name ends in `Directive` per the [style guide](style-guide.html#02-03 "Angular Style Guide").
510
548
Angular's own directives do not.
511
549
>>>>>>> docs(template-syntax/structural-directives): refresh both guides for style, accuracy, understanding
@@ -545,16 +583,33 @@ block unless-intro
545
583
The directive consumer expects to bind a condition to a directive property's `myUnless` input property.
546
584
Add the `@Input() myUnless` input property as a setter-only (there's no need to read it).
547
585
586
+ <<<<<<< a5705eb367d80adf19abf765d735b9d1189b52e6
548
587
<<<<<<< dc4da634c40560e4eda55814676bc96a230428dc
549
588
+ makeExample('structural-directives/ts/src/app/structural-directives.component.html' , 'myUnless' )( format ="." )
550
589
= ======
590
+ = ======
591
+ .alert.is-critical
592
+ :marked
593
+ Ward: This first sentence threw me at first. I had to read it several times. I
594
+ understand until the phrase "directive property's `myUnless` input property". I get
595
+ "`myUnless` input property", since you just showed it, but I don't understand
596
+ the relationship between the directive property...and the `@Input`? I don't
597
+ know if I'm wording that correctly.
598
+
599
+ For the snippet below, if I were following along to build my own, I'd have only a
600
+ general notion of what was being specified in it and think to mysef "ok, I'm going to
601
+ trust you on this one" and scroll down to hopefully find an example of it in action.
602
+ The translation below helps but thought you should know I'd feel a little in need of
603
+ bravery at first glance.
604
+
605
+ >>>>>>> docs(template-syntax/structural-directives): Add edits and comments on structural-directives
551
606
+ makeExample('structural-directives/ts/app/my-unless.directive.ts' , 'set' )( format ="." )
552
607
>>>>>>> docs(template-syntax/structural-directives): refresh both guides for style, accuracy, understanding
553
608
:marked
554
609
Angular calls this setter whenever the value of the condition changes.
555
610
556
611
* If the condition is falsy and the view hasn't been created previously,
557
- let the create the _view container_ create the _embedded view_ from the template.
612
+ let the _view container_ create the _embedded view_ from the template.
558
613
559
614
* If the condition is truthy and the view is currently displayed,
560
615
clear the container which also destroys the view.
@@ -612,7 +667,7 @@ a#summary
612
667
You learned
613
668
* that structural directives manipulate HTML layout.
614
669
* to use [`<ng-container>`](#ngcontainer) as a grouping element when there is no suitable host element.
615
- * that the angular "de-sugars" [asterisk (\*) syntax](#asterisk) into a `<template>`
616
- * how that works for the `NgIf`, `NgFor` and `NgSwitch` built-in directives
617
- * about the [_microsyntax_](#microsyntax) that expands into a [`<template>`](#template)
670
+ * that the angular "de-sugars" [asterisk (\*) syntax](#asterisk) into a `<template>`.
671
+ * how that works for the `NgIf`, `NgFor` and `NgSwitch` built-in directives.
672
+ * about the [_microsyntax_](#microsyntax) that expands into a [`<template>`](#template).
618
673
* to write a [custom structural directive](#unless), `myUnless`.
0 commit comments