Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit 3589242

Browse files
kapunahelewongwardbell
authored andcommitted
docs(template-syntax/structural-directives): Add edits and comments on structural-directives
1 parent a5705eb commit 3589242

File tree

1 file changed

+75
-20
lines changed

1 file changed

+75
-20
lines changed

public/docs/ts/latest/guide/structural-directives.jade

Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ style.
2727

2828
Try the <live-example></live-example>.
2929

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+
3039
a#definition
3140
.l-main-section
3241
:marked
@@ -49,9 +58,10 @@ a#definition
4958
You'll learn in this guide that the [asterisk (\*) is a convenience syntax](#asterisk). Angular "de-sugars" it into a `<template>` around the
5059
host element and its descendents. Each structural directive does something different with that template.
5160

52-
Three of the common, built-in structural directives &mdash;
53-
[NgIf](template-syntax.html#ngIf), [NgFor](template-syntax.html#ngFor), and [NgSwitch...](template-syntax.html#ngSwitch) &mdash;
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&mdash;[NgIf](template-syntax.html#ngIf),
62+
[NgFor](template-syntax.html#ngFor), and [NgSwitch...](template-syntax.html#ngSwitch)&mdash;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:
5565

5666
+makeExample('structural-directives/ts/app/app.component.html', 'built-in')(format=".")
5767
:marked
@@ -84,7 +94,7 @@ a#ngIf
8494
## NgIf Case Study
8595

8696
`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.
8898

8999
<<<<<<< dc4da634c40560e4eda55814676bc96a230428dc
90100
+makeExample('structural-directives/ts/src/app/structural-directives.component.html', 'ngIf')(format=".")
@@ -103,9 +113,9 @@ figure.image-display
103113
The top paragraph is in the DOM. The bottom, disused paragraph is not;
104114
in its place is a comment about "template bindings" (more about that [later](#asterisk)).
105115

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.
109119
The component and DOM nodes can be garbage-collected and free up memory.
110120

111121
### Why *remove* rather than *hide*?
@@ -126,20 +136,20 @@ figure.image-display
126136
Angular keeps checking for changes that could affect data bindings.
127137
Whatever the component was doing, it keeps doing.
128138

129-
Although invisible, the component &mdash; and all of its descendant components &mdash; tie up resources.
139+
Although invisible, the component&mdash;and all of its descendant components&mdash;tie up resources.
130140
The performance and memory burden can be substantial, responsiveness can degrade, and the user sees nothing.
131141

132142
On the positive side, showing the element again is quick.
133143
The component's previous state is preserved and ready to display.
134-
The component doesn't re-initialize &mdash; an operation that could be expensive.
144+
The component doesn't re-initialize&mdash;an operation that could be expensive.
135145
So hiding and showing is sometimes the right thing to do.
136146

137147
But in the absence of a compelling reason to keep them around,
138148
your preference should be to remove DOM elements that the user can't see
139149
and recover the unused resources with a structural directive like `NgIf` .
140150

141151
**These same considerations apply to every structural directive, whether built-in or custom.**
142-
We should ask ourselves &mdash; and the users of our directives &mdash; to think carefully
152+
We should ask ourselves&mdash;and the users of our directives&mdash;to think carefully
143153
about the consequences of adding and removing elements and of creating and destroying components.
144154

145155
a#ngcontainer
@@ -168,16 +178,16 @@ a#ng-container
168178
+makeExample('structural-directives/ts/app/app.component.html', 'ngif')(format=".")
169179

170180
:marked
171-
Introducing another container element &mdash; typically a `<span>` or `<div>` &mdash;
172-
to group the elements under a single _root_ is usually harmless.
181+
Introducing another container element&mdash;typically a `<span>` or `<div>`&mdash;to
182+
group the elements under a single _root_ is usually harmless.
173183
_Usually_ ... but not _always_.
174184

175185
The grouping element may break the template appearance because CSS styles
176186
neither expect nor accommodate the new layout.
177187
For example, suppose you have the following paragraph layout.
178188
+makeExample('structural-directives/ts/app/app.component.html', 'ngif-span')(format=".")
179189
: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.
181191
+makeExample('structural-directives/ts/app/app.component.css', 'p-span')(format=".")
182192
:marked
183193
The constructed paragraph renders strangely.
@@ -286,7 +296,7 @@ a#ngfor
286296
Angular transforms the `*ngFor` in similar fashion from asterisk (\*) syntax through
287297
template _attribute_ to template _element_.
288298

289-
Here's a full-featured `ngFor`, written all three ways
299+
Here's a full-featured `ngFor`, written all three ways:
290300
+makeExample('structural-directives/ts/app/app.component.html', 'inside-ngfor')(format=".")
291301

292302
<<<<<<< dc4da634c40560e4eda55814676bc96a230428dc
@@ -296,7 +306,7 @@ a#ngfor
296306
>>>>>>> docs(template-syntax/structural-directives): refresh both guides for style, accuracy, understanding
297307
:marked
298308
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.
300310
At minimum `NgFor` needs a looping variable (`let hero`) and a list (`heroes`).
301311

302312
You enable these features in the string assigned to `ngFor`, which you write in Angular's [microsyntax](microsyntax).
@@ -326,6 +336,14 @@ a#microsyntax
326336
The `NgFor` directive sets its _context's_ `$implicit` property to the current item in the list during each iteration.
327337
That becomes the current value of the `hero` variable in the example above.
328338

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
329347
* The other _let_ variables (`i` and `odd`) are linked to one of the directive's _named context_ properties.
330348
Look to the directive's documentation to see which properties are available and what they do.
331349
`NgFor` has several, including `index` and `odd`.
@@ -385,13 +403,17 @@ a#ngswitch
385403
:marked
386404
## Inside the _NgSwitch_ directives
387405

406+
<<<<<<< a5705eb367d80adf19abf765d735b9d1189b52e6
388407
<<<<<<< dc4da634c40560e4eda55814676bc96a230428dc
389408
+makeExample('structural-directives/ts/src/app/structural-directives.component.html', 'asterisk')(format=".")
390409
:marked
391410
We're prefixing these directive names with an asterisk (\*).
392411
=======
393412
The Angular _NgSwitch_ is actally a set of cooperating directives: `NgSwitch`, `NgSwitchCase`, and `NgSwitchDefault`.
394413
>>>>>>> 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
395417

396418
Here's an example.
397419
+makeExample('structural-directives/ts/app/app.component.html', 'ngswitch')(format=".")
@@ -409,6 +431,13 @@ a#ngswitch
409431
`NgSwitchDefault` includes its host element when no `NgSwitchCase` does.
410432
>>>>>>> docs(template-syntax/structural-directives): refresh both guides for style, accuracy, understanding
411433

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
412441
As with other structural directives, the `NgSwitchCase` and `NgSwitchDefault`
413442
can be "de-sugared" into the template _attribute_ form.
414443
+makeExample('structural-directives/ts/app/app.component.html', 'ngswitch-template-attr')(format=".")
@@ -461,7 +490,7 @@ a#unless
461490
## Write a structural directive
462491
In this section, you write a `MyUnless` directive structural directive
463492
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`.
465494
`MyUnless` displays the content when the condition is ***false***.
466495

467496
+makeExample('structural-directives/ts/app/app.component.html', 'myUnless-1')(format=".")
@@ -503,9 +532,18 @@ block unless-intro
503532
Pick something short that fits you or your company.
504533
In this example, the prefix is `my`.
505534

535+
<<<<<<< a5705eb367d80adf19abf765d735b9d1189b52e6
506536
<<<<<<< dc4da634c40560e4eda55814676bc96a230428dc
507537
+makeExample('structural-directives/ts/src/app/unless.directive.ts', 'unless-constructor')(format=".")
508538
=======
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
509547
The directive _class_ name ends in `Directive` per the [style guide](style-guide.html#02-03 "Angular Style Guide").
510548
Angular's own directives do not.
511549
>>>>>>> docs(template-syntax/structural-directives): refresh both guides for style, accuracy, understanding
@@ -545,16 +583,33 @@ block unless-intro
545583
The directive consumer expects to bind a condition to a directive property's `myUnless` input property.
546584
Add the `@Input() myUnless` input property as a setter-only (there's no need to read it).
547585

586+
<<<<<<< a5705eb367d80adf19abf765d735b9d1189b52e6
548587
<<<<<<< dc4da634c40560e4eda55814676bc96a230428dc
549588
+makeExample('structural-directives/ts/src/app/structural-directives.component.html', 'myUnless')(format=".")
550589
=======
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
551606
+makeExample('structural-directives/ts/app/my-unless.directive.ts', 'set')(format=".")
552607
>>>>>>> docs(template-syntax/structural-directives): refresh both guides for style, accuracy, understanding
553608
:marked
554609
Angular calls this setter whenever the value of the condition changes.
555610

556611
* 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.
558613

559614
* If the condition is truthy and the view is currently displayed,
560615
clear the container which also destroys the view.
@@ -612,7 +667,7 @@ a#summary
612667
You learned
613668
* that structural directives manipulate HTML layout.
614669
* 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).
618673
* to write a [custom structural directive](#unless), `myUnless`.

0 commit comments

Comments
 (0)