2016-05-04 08:18:38 -04:00
|
|
|
|
block includes
|
|
|
|
|
include ../_util-fns
|
2015-11-13 09:47:42 -05:00
|
|
|
|
|
|
|
|
|
:marked
|
|
|
|
|
One of the defining features of a single page application is its manipulation
|
|
|
|
|
of the DOM tree. Instead of serving a whole new page every time a user
|
2015-12-17 16:49:33 -05:00
|
|
|
|
navigates, whole sections of the DOM appear and disappear according
|
2015-11-13 09:47:42 -05:00
|
|
|
|
to the application state. In this chapter we'll to look at how Angular
|
|
|
|
|
manipulates the DOM and how we can do it ourselves in our own directives.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
In this chapter we will
|
|
|
|
|
- [learn what structural directives are](#definition)
|
2016-05-04 08:18:38 -04:00
|
|
|
|
- [study *ngIf*](#ngIf)
|
2015-12-17 16:49:33 -05:00
|
|
|
|
- [discover the <template> element](#template)
|
2015-11-13 09:47:42 -05:00
|
|
|
|
- [understand the asterisk (\*) in **ngFor*](#asterisk)
|
|
|
|
|
- [write our own structural directive](#unless)
|
2016-05-09 16:13:50 -04:00
|
|
|
|
p
|
|
|
|
|
| Try the #[+liveExampleLink2('live example', 'structural-directives')].
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
<a id="definition"></a>
|
|
|
|
|
.l-main-section
|
|
|
|
|
:marked
|
|
|
|
|
## What are structural directives?
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
There are three kinds of Angular directives:
|
|
|
|
|
1. Components
|
|
|
|
|
1. Attribute directives
|
|
|
|
|
1. Structural directives
|
|
|
|
|
|
2015-12-17 16:49:33 -05:00
|
|
|
|
The *Component* is really a directive with a template.
|
2015-11-13 09:47:42 -05:00
|
|
|
|
It's the most common of the three directives and we write lots of them as we build our application.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
The [*Attribute* directive](attribute-directives.html) changes the appearance or behavior of an element.
|
2016-01-03 13:03:37 -05:00
|
|
|
|
The built-in [NgStyle](template-syntax.html#ngStyle) directive, for example,
|
2015-12-17 16:49:33 -05:00
|
|
|
|
can change several element styles at the same time.
|
2015-11-13 09:47:42 -05:00
|
|
|
|
We can use it to render text bold, italic, and lime green by binding to a
|
|
|
|
|
component property that requests such a sickening result.
|
|
|
|
|
|
2015-12-17 16:49:33 -05:00
|
|
|
|
A *Structural* directive changes the DOM layout by adding and removing DOM elements.
|
2015-11-13 09:47:42 -05:00
|
|
|
|
We've seen three of the built-in structural directives in other chapters: [ngIf](template-syntax.html#ngIf),
|
2015-12-17 16:49:33 -05:00
|
|
|
|
[ngSwitch](template-syntax.html#ngSwitch) and [ngFor](template-syntax.html#ngFor).
|
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
+makeExample('structural-directives/ts/app/structural-directives.component.html', 'structural-directives')(format=".")
|
|
|
|
|
|
|
|
|
|
|
2016-05-04 08:18:38 -04:00
|
|
|
|
<a id="ngIf"></a>
|
2015-12-17 16:49:33 -05:00
|
|
|
|
.l-main-section
|
2015-11-13 09:47:42 -05:00
|
|
|
|
:marked
|
|
|
|
|
## NgIf Case Study
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
|
|
|
|
Let’s focus on `ngIf`. It's a great example of a structural
|
|
|
|
|
directive: it takes a boolean and makes an entire chunk of DOM appear
|
2015-11-13 09:47:42 -05:00
|
|
|
|
or disappear.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
+makeExample('structural-directives/ts/app/structural-directives.component.html', 'ngIf')(format=".")
|
|
|
|
|
|
|
|
|
|
:marked
|
2015-12-17 16:49:33 -05:00
|
|
|
|
The `ngIf` directive does not hide the element.
|
2015-11-13 09:47:42 -05:00
|
|
|
|
Using browser developer tools we can see that, when the condition is true, the top
|
2015-12-17 16:49:33 -05:00
|
|
|
|
paragraph is in the DOM and the bottom disused paragraph is completely
|
2015-11-13 09:47:42 -05:00
|
|
|
|
absent from the DOM! In its place are empty `<script>` tags.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
figure.image-display
|
|
|
|
|
img(src='/resources/images/devguide/structural-directives/element-not-in-dom.png' alt="element not in dom")
|
|
|
|
|
|
|
|
|
|
:marked
|
|
|
|
|
### Why *remove* rather than *hide*?
|
|
|
|
|
We could hide the unwanted paragraph by setting its css `display` style to `none`.
|
|
|
|
|
The element would remain in the DOM while invisible. Instead we removed it with `ngIf`.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
|
|
|
|
The difference matters. When we hide an element,
|
|
|
|
|
the component's behavior continues.
|
|
|
|
|
It remains attached to its DOM element. It continues to listen to events.
|
2015-11-13 09:47:42 -05:00
|
|
|
|
Angular keeps checking for changes that could affect data bindings.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
Whatever the component was doing it keeps doing.
|
|
|
|
|
|
|
|
|
|
Although invisible, the component — and all of its descendent components —
|
2015-11-13 09:47:42 -05:00
|
|
|
|
tie up resources that might be more useful elsewhere.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
The performance and memory burden can be substantial and the user may not benefit at all.
|
|
|
|
|
|
|
|
|
|
On the positive side, showing the element again is very quick.
|
2015-11-13 09:47:42 -05:00
|
|
|
|
The component's previous state is preserved and ready to display.
|
|
|
|
|
The component doesn't re-initialize — an operation that could be expensive.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
`ngIf` is different.
|
|
|
|
|
Setting `ngIf` to false **does** affect the component's resource consumption.
|
|
|
|
|
Angular removes the element from DOM, stops change detection for the associated component,
|
|
|
|
|
detaches it from DOM events (the attachments that it made) and destroys the component.
|
|
|
|
|
The component can be garbage-collected (we hope) and free up memory.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
Components often have child components which themselves have children.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
All of them are destroyed when `ngIf` destroys the common ancestor.
|
|
|
|
|
This cleanup effort is usually a good thing.
|
|
|
|
|
|
|
|
|
|
Of course it isn't *always* a good thing.
|
|
|
|
|
It might be a bad thing if we need that particular component again soon.
|
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
The component's state might be expensive to re-construct.
|
|
|
|
|
When `ngIf` becomes `true` again, Angular recreates the component and its subtree.
|
|
|
|
|
Angular runs every component's initialization logic again. That could be expensive ... as when
|
|
|
|
|
a component re-fetches data that had been in memory just moments ago.
|
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
2015-12-17 16:49:33 -05:00
|
|
|
|
*Design thought*: minimize initialization effort and consider caching state in a
|
2015-11-13 09:47:42 -05:00
|
|
|
|
companion service.
|
|
|
|
|
:marked
|
2015-12-17 16:49:33 -05:00
|
|
|
|
Although there are pros and cons to each approach,
|
2015-11-13 09:47:42 -05:00
|
|
|
|
in general it is best to use `ngIf` to remove unwanted components rather than
|
|
|
|
|
hide them.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
**These same considerations apply to every structural directive, whether built-in or custom.**
|
|
|
|
|
We should ask ourselves — and the users of our directives — to think carefully
|
|
|
|
|
about the consequences of adding and removing elements and of creating and destroying components.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
Let's see these dynamics at work. For fun, we'll stack the deck *against*
|
2015-12-17 16:49:33 -05:00
|
|
|
|
our recommendation and consider a component called `heavy-loader` that
|
2015-11-13 09:47:42 -05:00
|
|
|
|
***pretends*** to load a ton of data when initialized.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
We'll display two instances of the component. We toggle the visibility of the first one with CSS.
|
|
|
|
|
We toggle the second into and out of the DOM with `ngIf`.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
+makeTabs(
|
|
|
|
|
`structural-directives/ts/app/structural-directives.component.html,
|
|
|
|
|
structural-directives/ts/app/heavy-loader.component.ts`,
|
|
|
|
|
'message-log,',
|
2016-05-06 10:42:01 -04:00
|
|
|
|
'template (excerpt), heavy-loader.component.ts')
|
2015-11-13 09:47:42 -05:00
|
|
|
|
|
|
|
|
|
:marked
|
2015-12-17 16:49:33 -05:00
|
|
|
|
We also log when a component is created or destroyed
|
|
|
|
|
using the built-in `ngOnInit` and `ngOnDestroy` [lifecycle hooks](lifecycle-hooks.html).
|
2015-11-13 09:47:42 -05:00
|
|
|
|
Here it is in action:
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
figure.image-display
|
|
|
|
|
img(src='/resources/images/devguide/structural-directives/heavy-loader-toggle.gif' alt="heavy loader toggle")
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
:marked
|
2015-12-17 16:49:33 -05:00
|
|
|
|
Both components are in the DOM at the start.
|
2015-11-13 09:47:42 -05:00
|
|
|
|
First we toggle the component's visibility repeatedly. The component never leaves the DOM.
|
|
|
|
|
When visible it's always the same instance and the log is quiet.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
|
|
|
|
Then we toggle the second component with `ngIf`.
|
2015-11-13 09:47:42 -05:00
|
|
|
|
We create a new instance every time and the log shows that we're paying
|
2015-12-17 16:49:33 -05:00
|
|
|
|
a heavy price to create and destroy it.
|
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
If we really expected to "wink" the component like this, toggling visibility would be the better choice.
|
|
|
|
|
In most UIs, when we "close" a component we're unlikely see it again for a long time, if ever.
|
|
|
|
|
The `ngIf` would be preferred in that case.
|
|
|
|
|
|
|
|
|
|
<a id="template"></a>
|
|
|
|
|
.l-main-section
|
|
|
|
|
:marked
|
|
|
|
|
## The *<template>* tag
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
|
|
|
|
Structural directives, like `ngIf`, do their magic by using the
|
2015-11-13 09:47:42 -05:00
|
|
|
|
[HTML 5 template tag](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template).
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
Outside of an Angular app, the `<template>` tag's default CSS `display` property is `none`.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
It's contents are ***invisible*** within
|
2015-11-13 09:47:42 -05:00
|
|
|
|
a hidden [document fragment](https://developer.mozilla.org/en/docs/Web/API/DocumentFragment).
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
Inside of an app, Angular ***removes*** the`<template>` tags and their children.
|
|
|
|
|
The contents are gone — but not forgotten as we'll see soon.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
|
|
|
|
We can confirm these effects by wrapping the middle "hip" of the phrase "Hip! Hip! Hooray!" within a `<template>` tag.
|
|
|
|
|
+makeExample('structural-directives/ts/app/structural-directives.component.html', 'template-tag')(format=".")
|
2015-11-13 09:47:42 -05:00
|
|
|
|
:marked
|
2016-05-04 08:18:38 -04:00
|
|
|
|
The display is a 'Hip! Hooray!', short of perfect enthusiasm. The DOM effects are different when Angular is in control.
|
2015-11-13 09:47:42 -05:00
|
|
|
|
figure.image-display
|
2015-12-17 16:49:33 -05:00
|
|
|
|
img(src='/resources/images/devguide/structural-directives/template-in-out-of-a2.png' alt="template outside angular")
|
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
:marked
|
2015-12-17 16:49:33 -05:00
|
|
|
|
Evidently Angular replaces the `<template>` tag and its contents with empty `<script>` tags.
|
|
|
|
|
That's just its default behavior.
|
2015-11-13 09:47:42 -05:00
|
|
|
|
It can do something different as we saw when applying a variety of `ngSwitch` directives to `<template>` tags:
|
|
|
|
|
|
|
|
|
|
+makeExample('structural-directives/ts/app/structural-directives.component.html', 'ngSwitch')(format=".")
|
|
|
|
|
:marked
|
|
|
|
|
When one of those `ngSwitch` conditions is true, Angular inserts the template's content into the DOM.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
What does this have to do with `ngIf` and `ngFor`? We didn't use a `<template>` tag with those directives.
|
|
|
|
|
|
|
|
|
|
<a id="asterisk"></a>
|
|
|
|
|
.l-main-section
|
|
|
|
|
:marked
|
|
|
|
|
## The asterisk (\*) effect
|
|
|
|
|
Here are those directives again. See the difference?
|
|
|
|
|
|
|
|
|
|
+makeExample('structural-directives/ts/app/structural-directives.component.html', 'asterisk')(format=".")
|
|
|
|
|
:marked
|
2015-12-17 16:49:33 -05:00
|
|
|
|
We're prefixing these directive names with an asterisk (\*).
|
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
The asterisk is "syntactic sugar". It simplifies `ngIf` and `ngFor` for both the writer and the reader.
|
|
|
|
|
Under the hood, Angular replaces the asterisk version with a more verbose `<template>` form.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
The next two `ngIf` examples are effectively the same and we may write in either style:
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
+makeExample('structural-directives/ts/app/structural-directives.component.html', 'ngIf-template')(format=".")
|
|
|
|
|
|
|
|
|
|
:marked
|
|
|
|
|
Most of us would rather write in style (A).
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
|
|
|
|
It's worth knowing that Angular expands style (A) into style (B).
|
|
|
|
|
It moves the paragraph and its contents inside a `<template>` tag.
|
2015-11-13 09:47:42 -05:00
|
|
|
|
It moves the directive up to the `<template>` tag where it becomes a property binding,
|
|
|
|
|
surrounded in square brackets. The boolean value of the host component's `condition` property
|
|
|
|
|
determines whether the templated content is displayed or not.
|
|
|
|
|
|
|
|
|
|
Angular transforms `*ngFor` in a similar manner:
|
|
|
|
|
|
|
|
|
|
+makeExample('structural-directives/ts/app/structural-directives.component.html', 'ngFor-template')(format=".")
|
|
|
|
|
:marked
|
2015-12-17 16:49:33 -05:00
|
|
|
|
The basic pattern is the same: create a `<template>`, relocate the content,
|
|
|
|
|
and move the directive onto the `<template>`.
|
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
There are extra nuances stemming from
|
2016-04-28 14:18:52 -04:00
|
|
|
|
Angular's [ngFor micro-syntax](template-syntax.html#ngForMicrosyntax) which expands
|
2015-11-13 09:47:42 -05:00
|
|
|
|
into an additional `ngForOf` property binding (the iterable) and
|
2016-04-28 14:18:52 -04:00
|
|
|
|
the `hero` template input variable (the current item in each iteration).
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
|
|
|
|
<a id="unless"></a>
|
2015-11-13 09:47:42 -05:00
|
|
|
|
.l-main-section
|
|
|
|
|
:marked
|
|
|
|
|
## Make a structural directive
|
|
|
|
|
Let's write our own structural directive, an `Unless` directive, the not-so-evil twin of `ngIf`.
|
|
|
|
|
|
2015-12-17 16:49:33 -05:00
|
|
|
|
Unlike `ngIf` which displays the template content when `true`,
|
|
|
|
|
our directive displays the content when the condition is ***false***.
|
2015-11-13 09:47:42 -05:00
|
|
|
|
|
2016-05-04 08:18:38 -04:00
|
|
|
|
block unless-intro
|
|
|
|
|
:marked
|
|
|
|
|
Creating a directive is similar to creating a component.
|
|
|
|
|
* import the `Directive` decorator.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2016-05-04 08:18:38 -04:00
|
|
|
|
* add a CSS **attribute selector** (in brackets) that identifies our directive.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2016-05-04 08:18:38 -04:00
|
|
|
|
* specify the name of the public `input` property for binding
|
|
|
|
|
(typically the name of the directive itself).
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2016-05-04 08:18:38 -04:00
|
|
|
|
* apply the decorator to our implementation class.
|
2015-11-13 09:47:42 -05:00
|
|
|
|
|
2016-05-04 08:18:38 -04:00
|
|
|
|
Here is how we begin:
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
+makeExample('structural-directives/ts/app/unless.directive.ts', 'unless-declaration', 'unless.directive.ts (excerpt)')(format=".")
|
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
|
|
|
|
### Selector brackets [ ]
|
|
|
|
|
The CSS syntax for selecting an attribute is a name in square brackets.
|
2015-12-16 15:20:50 -05:00
|
|
|
|
We surround our directive name in square brackets. See *Directive configuration* on the
|
|
|
|
|
[cheatsheet](cheatsheet.html).
|
2015-11-13 09:47:42 -05:00
|
|
|
|
|
|
|
|
|
### Selector name prefixes
|
|
|
|
|
|
|
|
|
|
We recommend picking a selector name with a prefix to ensure
|
|
|
|
|
that it cannot conflict with any standard HTML attribute, now or in the future.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
|
|
|
|
We do **not** prefix our `unless` directive name with **`ng`**.
|
|
|
|
|
That prefix belongs to Angular and
|
2015-11-13 09:47:42 -05:00
|
|
|
|
we don't want to confuse our directives with their directives.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
Our prefix is `my`.
|
|
|
|
|
:marked
|
|
|
|
|
We'll need access to the template *and* something that can render its contents.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
We access the template with a `TemplateRef`. The renderer is a `ViewContainerRef`.
|
2015-11-13 09:47:42 -05:00
|
|
|
|
We inject both into our constructor as private variables.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
+makeExample('structural-directives/ts/app/unless.directive.ts', 'unless-constructor')(format=".")
|
|
|
|
|
|
|
|
|
|
:marked
|
2016-05-04 08:18:38 -04:00
|
|
|
|
The consumer of our directive will bind a boolean value to our directive's `myUnless` input property.
|
2015-11-13 09:47:42 -05:00
|
|
|
|
The directive adds or removes the template based on that value.
|
|
|
|
|
|
2016-05-04 08:18:38 -04:00
|
|
|
|
Let's add the `myUnless` property now as a setter-only property.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
+makeExample('structural-directives/ts/app/unless.directive.ts', 'unless-set')(format=".")
|
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
2015-12-17 16:49:33 -05:00
|
|
|
|
The `@Input()` annotation marks this property as an input for the directive.
|
2015-11-13 09:47:42 -05:00
|
|
|
|
:marked
|
2015-12-17 16:49:33 -05:00
|
|
|
|
Nothing fancy here: if the condition is false,
|
2015-11-13 09:47:42 -05:00
|
|
|
|
we render the template, otherwise we clear the element content.
|
|
|
|
|
|
2016-05-04 08:18:38 -04:00
|
|
|
|
The end result should look like this:
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
+makeExample('structural-directives/ts/app/unless.directive.ts', null, 'unless.directive.ts')
|
|
|
|
|
|
|
|
|
|
:marked
|
2015-12-17 16:49:33 -05:00
|
|
|
|
Now we add it to the `directives`array of the host component and try it.
|
|
|
|
|
First we add some test HTML to the template:
|
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
+makeExample('structural-directives/ts/app/structural-directives.component.html', 'myUnless')(format=".")
|
|
|
|
|
:marked
|
2015-12-17 16:49:33 -05:00
|
|
|
|
We run it and it behaves as expected, doing the opposite of `ngIf`.
|
2015-11-13 09:47:42 -05:00
|
|
|
|
When `condition` is `true`, the top paragraph is removed (replaced by `<script>` tags) and the bottom paragraph appears.
|
|
|
|
|
figure.image-display
|
|
|
|
|
img(src='/resources/images/devguide/structural-directives/myUnless-is-true.png' alt="myUnless is true" )
|
|
|
|
|
|
|
|
|
|
:marked
|
2015-12-17 16:49:33 -05:00
|
|
|
|
Our `myUnless` directive is dead simple. Surely we left something out.
|
|
|
|
|
Surely `ngIf` is more complex?
|
|
|
|
|
|
2016-05-04 08:18:38 -04:00
|
|
|
|
[Look at the source code](https://github.com/angular/angular/blob/master/modules/%40angular/common/src/directives/ng_if.ts).
|
2015-11-13 09:47:42 -05:00
|
|
|
|
It's well documented and we shouldn't be shy
|
|
|
|
|
about consulting the source when we want to know how something works.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
`ngIf` isn't much different! There are a few
|
|
|
|
|
additional checks to improve performance (don't clear or recreate the
|
|
|
|
|
view unless necessary) but otherwise it's much the same.
|
|
|
|
|
|
2015-12-17 16:49:33 -05:00
|
|
|
|
.l-main-section
|
2015-11-13 09:47:42 -05:00
|
|
|
|
:marked
|
|
|
|
|
## Wrap up
|
2015-12-17 16:49:33 -05:00
|
|
|
|
Here is the pertinent source for this chapter.
|
2015-11-13 09:47:42 -05:00
|
|
|
|
|
|
|
|
|
+makeTabs(`
|
|
|
|
|
structural-directives/ts/app/unless.directive.ts,
|
2015-12-17 16:49:33 -05:00
|
|
|
|
structural-directives/ts/app/heavy-loader.component.ts,
|
2015-11-13 09:47:42 -05:00
|
|
|
|
structural-directives/ts/app/structural-directives.component.ts,
|
|
|
|
|
structural-directives/ts/app/structural-directives.component.html
|
2015-12-17 16:49:33 -05:00
|
|
|
|
`,
|
|
|
|
|
null,
|
2015-11-13 09:47:42 -05:00
|
|
|
|
`unless.directive.ts,
|
2015-12-17 16:49:33 -05:00
|
|
|
|
heavy-loader.component.ts,
|
2015-11-13 09:47:42 -05:00
|
|
|
|
structural-directives.component.ts,
|
|
|
|
|
structural-directives.component.html
|
|
|
|
|
`)
|
|
|
|
|
:marked
|
2015-12-17 16:49:33 -05:00
|
|
|
|
We learned that we can manipulate our HTML layout with
|
2015-11-13 09:47:42 -05:00
|
|
|
|
structural directives like `ngFor` and `ngIf` and we
|
|
|
|
|
wrote our own structural directive, `myUnless`, to do something similar.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
Angular offers more sophisticated techniques for managing layout
|
|
|
|
|
such as *structural components* that can take external content
|
2015-12-17 16:49:33 -05:00
|
|
|
|
and incorporate that content within their own templates.
|
2015-11-13 09:47:42 -05:00
|
|
|
|
Tab and tab pane controls are good examples.
|
2015-12-17 16:49:33 -05:00
|
|
|
|
|
2015-11-13 09:47:42 -05:00
|
|
|
|
We'll learn about structural components in a future chapter.
|