2017-05-26 15:28:06 -04:00
|
|
|
|
# Structural Directives
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
2017-07-29 12:03:22 -04:00
|
|
|
|
# 结构型指令
|
2017-03-30 15:04:18 -04:00
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
<style>
|
2017-03-31 19:57:13 -04:00
|
|
|
|
h4 {font-size: 17px !important; text-transform: none !important;}
|
|
|
|
|
.syntax { font-family: Consolas, 'Lucida Sans', Courier, sans-serif; color: black; font-size: 85%; }
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
</style>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-04-26 08:11:02 -04:00
|
|
|
|
This guide looks at how Angular manipulates the DOM with **structural directives** and
|
2017-02-22 13:09:39 -05:00
|
|
|
|
how you can write your own structural directives to do the same thing.
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
在本章中,我们将看看Angular如何用*结构型指令*操纵DOM树,以及我们该如何写自己的结构型指令来完成同样的任务。
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
Try the <live-example></live-example>.
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
试试<live-example></live-example>。
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
|
|
|
|
{@a definition}
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
## What are structural directives?
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
## 什么是结构型指令?
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
Structural directives are responsible for HTML layout.
|
|
|
|
|
They shape or reshape the DOM's _structure_, typically by adding, removing, or manipulating
|
|
|
|
|
elements.
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
结构型指令的职责是HTML布局。
|
|
|
|
|
它们塑造或重塑DOM的结构,比如添加、移除或维护这些元素。
|
|
|
|
|
|
2017-04-26 08:11:02 -04:00
|
|
|
|
As with other directives, you apply a structural directive to a _host element_.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
The directive then does whatever it's supposed to do with that host element and its descendents.
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
像其它指令一样,你可以把结构型指令应用到一个*宿主元素*上。
|
|
|
|
|
然后它就可以对宿主元素及其子元素做点什么。
|
|
|
|
|
|
2017-04-26 08:11:02 -04:00
|
|
|
|
Structural directives are easy to recognize.
|
2017-03-11 08:44:25 -05:00
|
|
|
|
An asterisk (*) precedes the directive attribute name as in this example.
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
结构型指令非常容易识别。
|
|
|
|
|
在这个例子中,星号(*)被放在指令的属性名之前。
|
|
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
|
|
<code-example path="structural-directives/src/app/app.component.html" linenums="false" title="src/app/app.component.html (ngif)" region="ngif">
|
|
|
|
|
|
|
|
|
|
</code-example>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
No brackets. No parentheses. Just `*ngIf` set to a string.
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
没有方括号,没有圆括号,只是把`*ngIf`设置为一个字符串。
|
|
|
|
|
|
2017-03-11 10:36:40 -05:00
|
|
|
|
You'll learn in this guide that the [asterisk (*) is a convenience notation](guide/structural-directives#asterisk)
|
2017-07-22 23:51:25 -04:00
|
|
|
|
and the string isa [_microsyntax_](guide/structural-directives#microsyntax) rather than the usual
|
2017-03-31 19:57:13 -04:00
|
|
|
|
[template expression](guide/template-syntax#template-expressions).
|
2017-04-12 15:53:18 -04:00
|
|
|
|
Angular desugars this notation into a marked-up `<ng-template>` that surrounds the
|
2017-04-26 08:11:02 -04:00
|
|
|
|
host element and its descendents.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
Each structural directive does something different with that template.
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
在这个例子中,我们将学到[星号(*)这个简写方法](guide/structural-directives#asterisk),而这个字符串是一个[*微语法*](guide/structural-directives#microsyntax),而不是通常的[模板表达式](guide/template-syntax#template-expressions)。
|
|
|
|
|
Angular会解开这个语法糖,变成一个`<ng-template>`标记,包裹着宿主元素及其子元素。
|
|
|
|
|
每个结构型指令都可以用这个模板做点不同的事情。
|
|
|
|
|
|
2017-04-26 08:11:02 -04:00
|
|
|
|
Three of the common, built-in structural directives—[NgIf](guide/template-syntax#ngIf),
|
|
|
|
|
[NgFor](guide/template-syntax#ngFor), and [NgSwitch...](guide/template-syntax#ngSwitch)—are
|
|
|
|
|
described in the [_Template Syntax_](guide/template-syntax) guide and seen in samples throughout the Angular documentation.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
Here's an example of them in a template:
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
三个常用的内置结构型指令 —— [NgIf](guide/template-syntax#ngIf)、[NgFor](guide/template-syntax#ngFor)和[NgSwitch...](guide/template-syntax#ngSwitch)。
|
|
|
|
|
我们在[*模板语法*](guide/template-syntax)一章中讲过它,并且在Angular文档的例子中到处都在用它。下面是模板中的例子:
|
|
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
|
|
<code-example path="structural-directives/src/app/app.component.html" linenums="false" title="src/app/app.component.html (built-in)" region="built-in">
|
|
|
|
|
|
|
|
|
|
</code-example>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
This guide won't repeat how to _use_ them. But it does explain _how they work_
|
2017-03-11 10:36:40 -05:00
|
|
|
|
and how to [write your own](guide/structural-directives#unless) structural directive.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
本章不会重复讲如何*使用*它们,而是解释它们的*工作原理*以及如何[写自己的结构型指令](guide/structural-directives#unless)。
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-04-10 11:51:13 -04:00
|
|
|
|
<div class="callout is-helpful">
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
|
|
|
|
|
2017-03-30 15:04:18 -04:00
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
<header>
|
|
|
|
|
Directive spelling
|
|
|
|
|
</header>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
<header>
|
|
|
|
|
指令的拼写形式
|
|
|
|
|
</header>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
Throughout this guide, you'll see a directive spelled in both _UpperCamelCase_ and _lowerCamelCase_.
|
|
|
|
|
Already you've seen `NgIf` and `ngIf`.
|
2017-04-26 08:11:02 -04:00
|
|
|
|
There's a reason. `NgIf` refers to the directive _class_;
|
|
|
|
|
`ngIf` refers to the directive's _attribute name_.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
在本章中,我们将看到指令同时具有两种拼写形式*大驼峰`UpperCamelCase`和小驼峰`lowerCamelCase`,比如我们已经看过的`NgIf`和`ngIf`。
|
|
|
|
|
这里的原因在于,`NgIf`引用的是指令的*类名*,而`ngIf`引用的是指令的*属性名*。
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
A directive _class_ is spelled in _UpperCamelCase_ (`NgIf`).
|
|
|
|
|
A directive's _attribute name_ is spelled in _lowerCamelCase_ (`ngIf`).
|
|
|
|
|
The guide refers to the directive _class_ when talking about its properties and what the directive does.
|
|
|
|
|
The guide refers to the _attribute name_ when describing how
|
|
|
|
|
you apply the directive to an element in the HTML template.
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
指令的*类名*拼写成*大驼峰形式*(`NgIf`),而它的*属性名*则拼写成*小驼峰形式*(`ngIf`)。
|
|
|
|
|
本章会在谈论指令的属性和工作原理时引用指令的*类名*,在描述如何在HTML模板中把该指令应用到元素时,引用指令的*属性名*。
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-04-10 11:51:13 -04:00
|
|
|
|
</div>
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
|
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
2017-04-10 11:51:13 -04:00
|
|
|
|
<div class="l-sub-section">
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
There are two other kinds of Angular directives, described extensively elsewhere:
|
|
|
|
|
(1) components and (2) attribute directives.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
还有另外两种Angular指令,在本开发指南的其它地方有讲解:(1) 组件 (2) 属性型指令。
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
A *component* manages a region of HTML in the manner of a native HTML element.
|
2017-04-26 08:11:02 -04:00
|
|
|
|
Technically it's a directive with a template.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
*组件*可以在原生HTML元素中管理一小片区域的HTML。从技术角度说,它就是一个带模板的指令。
|
|
|
|
|
|
2017-03-11 10:36:40 -05:00
|
|
|
|
An [*attribute* directive](guide/attribute-directives) changes the appearance or behavior
|
2017-02-22 13:09:39 -05:00
|
|
|
|
of an element, component, or another directive.
|
2017-03-31 19:57:13 -04:00
|
|
|
|
For example, the built-in [`NgStyle`](guide/template-syntax#ngStyle) directive
|
2017-02-22 13:09:39 -05:00
|
|
|
|
changes several element styles at the same time.
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
[*属性型*指令](guide/attribute-directives)会改变某个元素、组件或其它指令的外观或行为。
|
|
|
|
|
比如,内置的[`NgStyle`](guide/template-syntax#ngStyle)指令可以同时修改元素的多个样式。
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
You can apply many _attribute_ directives to one host element.
|
2017-03-11 10:36:40 -05:00
|
|
|
|
You can [only apply one](guide/structural-directives#one-per-element) _structural_ directive to a host element.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
我们可以在一个宿主元素上应用多个*属性型*指令,但[只能应用一个](guide/structural-directives#one-per-element)*结构型*指令。
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-04-10 11:51:13 -04:00
|
|
|
|
</div>
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
{@a ngIf}
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
## NgIf case study
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
## NgIf案例分析
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
`NgIf` is the simplest structural directive and the easiest to understand.
|
2017-03-11 08:44:25 -05:00
|
|
|
|
It takes a boolean expression and makes an entire chunk of the DOM appear or disappear.
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
我们重点看下`ngIf`。它是一个很好的结构型指令案例:它接受一个布尔值,并据此让一整块DOM树出现或消失。
|
|
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
|
|
<code-example path="structural-directives/src/app/app.component.html" linenums="false" title="src/app/app.component.html (ngif-true)" region="ngif-true">
|
|
|
|
|
|
|
|
|
|
</code-example>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
The `ngIf` directive doesn't hide elements with CSS. It adds and removes them physically from the DOM.
|
|
|
|
|
Confirm that fact using browser developer tools to inspect the DOM.
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
`ngIf`指令并不是使用CSS来隐藏元素的。它会把这些元素从DOM中物理删除。
|
|
|
|
|
使用浏览器的开发者工具就可以确认这一点。
|
|
|
|
|
|
2017-03-30 15:04:18 -04:00
|
|
|
|
|
2017-05-09 18:53:32 -04:00
|
|
|
|
<figure>
|
|
|
|
|
<img src='generated/images/guide/structural-directives/element-not-in-dom.png' alt="ngIf=false element not in DOM">
|
2017-02-22 13:09:39 -05:00
|
|
|
|
</figure>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-04-26 08:11:02 -04:00
|
|
|
|
The top paragraph is in the DOM. The bottom, disused paragraph is not;
|
2017-04-12 15:53:18 -04:00
|
|
|
|
in its place is a comment about "bindings" (more about that [later](guide/structural-directives#asterisk)).
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
可以看到第一段文字出现在了DOM中,而第二段则没有,在第二段的位置上是一个关于“绑定”的注释([稍后](guide/structural-directives#asterisk)有更多讲解)。
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
When the condition is false, `NgIf` removes its host element from the DOM,
|
|
|
|
|
detaches it from DOM events (the attachments that it made),
|
|
|
|
|
detaches the component from Angular change detection, and destroys it.
|
|
|
|
|
The component and DOM nodes can be garbage-collected and free up memory.
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
当条件为假时,`NgIf`会从DOM中移除它的宿主元素,取消它监听过的那些DOM事件,从Angular变更检测中移除该组件,并销毁它。
|
|
|
|
|
这些组件和DOM节点可以被当做垃圾收集起来,并且释放它们占用的内存。
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
### Why *remove* rather than *hide*?
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
### 为什么*移除*而不是*隐藏*?
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
A directive could hide the unwanted paragraph instead by setting its `display` style to `none`.
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
指令也可以通过把它的`display`风格设置为`none`而隐藏不需要的段落。
|
|
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
|
|
<code-example path="structural-directives/src/app/app.component.html" linenums="false" title="src/app/app.component.html (display-none)" region="display-none">
|
|
|
|
|
|
|
|
|
|
</code-example>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-04-26 08:11:02 -04:00
|
|
|
|
While invisible, the element remains in the DOM.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
当不可见时,这个元素仍然留在DOM中。
|
|
|
|
|
|
2017-03-30 15:04:18 -04:00
|
|
|
|
|
2017-05-09 18:53:32 -04:00
|
|
|
|
<figure>
|
|
|
|
|
<img src='generated/images/guide/structural-directives/element-display-in-dom.png' alt="hidden element still in DOM">
|
2017-02-22 13:09:39 -05:00
|
|
|
|
</figure>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-04-26 08:11:02 -04:00
|
|
|
|
The difference between hiding and removing doesn't matter for a simple paragraph.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
It does matter when the host element is attached to a resource intensive component.
|
|
|
|
|
Such a component's behavior continues even when hidden.
|
|
|
|
|
The component stays attached to its DOM element. It keeps listening to events.
|
|
|
|
|
Angular keeps checking for changes that could affect data bindings.
|
|
|
|
|
Whatever the component was doing, it keeps doing.
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
对于简单的段落,隐藏和移除之间的差异影响不大,但对于资源占用较多的组件是不一样的。当我们隐藏掉一个元素时,组件的行为还在继续 —— 它仍然附加在它所属的DOM元素上,
|
|
|
|
|
它也仍在监听事件。Angular会继续检查哪些能影响数据绑定的变更。
|
|
|
|
|
组件原本要做的那些事情仍在继续。
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
Although invisible, the component—and all of its descendant components—tie up resources.
|
|
|
|
|
The performance and memory burden can be substantial, responsiveness can degrade, and the user sees nothing.
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
虽然不可见,组件及其各级子组件仍然占用着资源,而这些资源如果分配给别人可能会更有用。
|
|
|
|
|
在性能和内存方面的负担相当可观,响应度会降低,而用户却可能无法从中受益。
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
On the positive side, showing the element again is quick.
|
|
|
|
|
The component's previous state is preserved and ready to display.
|
|
|
|
|
The component doesn't re-initialize—an operation that could be expensive.
|
|
|
|
|
So hiding and showing is sometimes the right thing to do.
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
当然,从积极的一面看,重新显示这个元素会非常快。
|
|
|
|
|
组件以前的状态被保留着,并随时可以显示。
|
|
|
|
|
组件不用重新初始化 —— 该操作可能会比较昂贵。
|
|
|
|
|
这时候隐藏和显示就成了正确的选择。
|
|
|
|
|
|
2017-04-26 08:11:02 -04:00
|
|
|
|
But in the absence of a compelling reason to keep them around,
|
2017-02-22 13:09:39 -05:00
|
|
|
|
your preference should be to remove DOM elements that the user can't see
|
|
|
|
|
and recover the unused resources with a structural directive like `NgIf` .
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
但是,除非有非常强烈的理由来保留它们,否则我们更倾向于移除用户看不见的那些DOM元素,并且使用`NgIf`这样的结构型指令来收回用不到的资源。
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
**These same considerations apply to every structural directive, whether built-in or custom.**
|
2017-04-26 08:11:02 -04:00
|
|
|
|
Before applying a structural directive, you might want to pause for a moment
|
2017-02-22 13:09:39 -05:00
|
|
|
|
to consider the consequences of adding and removing elements and of creating and destroying components.
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
**同样的考量也适用于每一个结构型指令,无论是内置的还是自定义的。**
|
|
|
|
|
我们应该提醒自己以及我们指令的使用者,来仔细考虑添加元素、移除元素以及创建和销毁组件的后果。
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
|
|
|
|
{@a asterisk}
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
## The asterisk (*) prefix
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
## 星号(*)前缀
|
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
Surely you noticed the asterisk (*) prefix to the directive name
|
2017-02-22 13:09:39 -05:00
|
|
|
|
and wondered why it is necessary and what it does.
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
你可能注意到了指令名的星号(*)前缀,并且困惑于为什么需要它以及它是做什么的。
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
Here is `*ngIf` displaying the hero's name if `hero` exists.
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
这里的`*ngIf`会在`hero`存在时显示英雄的名字。
|
|
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
|
|
<code-example path="structural-directives/src/app/app.component.html" linenums="false" title="src/app/app.component.html (asterisk)" region="asterisk">
|
|
|
|
|
|
|
|
|
|
</code-example>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
The asterisk is "syntactic sugar" for something a bit more complicated.
|
2017-03-11 08:44:25 -05:00
|
|
|
|
Internally, Angular desugars it in two stages.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
First, it translates the `*ngIf="..."` into a template _attribute_, `template="ngIf ..."`, like this.
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
星号是一个用来简化更复杂语法的“语法糖”。
|
|
|
|
|
从内部实现来说,Angular会分两个阶段解开这个语法糖。
|
|
|
|
|
首先,它把`*ngIf="..."`翻译成一个`template`*属性* `template="ngIf ..."`,代码如下:
|
|
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
|
|
<code-example path="structural-directives/src/app/app.component.html" linenums="false" title="src/app/app.component.html (ngif-template-attr)" region="ngif-template-attr">
|
|
|
|
|
|
|
|
|
|
</code-example>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-04-12 15:53:18 -04:00
|
|
|
|
Then it translates the template _attribute_ into a `<ng-template>` _element_, wrapped around the host element, like this.
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
然后,它把这个`template`*属性*翻译成一个`<ng-template>`*元素*,并用它包裹宿主元素,代码如下:
|
|
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
|
|
<code-example path="structural-directives/src/app/app.component.html" linenums="false" title="src/app/app.component.html (ngif-template)" region="ngif-template">
|
|
|
|
|
|
|
|
|
|
</code-example>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-04-12 15:53:18 -04:00
|
|
|
|
* The `*ngIf` directive moved to the `<ng-template>` element where it became a property binding,`[ngIf]`.
|
2017-07-22 23:51:25 -04:00
|
|
|
|
|
|
|
|
|
`*ngIf`指令被移到了`<ng-template>`元素上。在那里它变成了一个属性绑定`[ngIf]`。
|
|
|
|
|
|
2017-04-12 15:53:18 -04:00
|
|
|
|
* The rest of the `<div>`, including its class attribute, moved inside the `<ng-template>` element.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
`<div>`上的其余部分,包括它的`class`属性在内,移到了内部的`<ng-template>`元素上。
|
|
|
|
|
|
2017-04-26 08:11:02 -04:00
|
|
|
|
None of these forms are actually rendered.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
Only the finished product ends up in the DOM.
|
2017-03-11 08:44:25 -05:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
上述形式永远不会真的渲染出来。
|
|
|
|
|
只有最终产出的结果才会出现在DOM中。
|
|
|
|
|
|
2017-03-30 15:04:18 -04:00
|
|
|
|
|
2017-05-09 18:53:32 -04:00
|
|
|
|
<figure>
|
|
|
|
|
<img src='generated/images/guide/structural-directives/hero-div-in-dom.png' alt="hero div in DOM">
|
2017-02-22 13:09:39 -05:00
|
|
|
|
</figure>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-04-26 08:11:02 -04:00
|
|
|
|
Angular consumed the `<ng-template>` content during its actual rendering and
|
2017-04-12 15:53:18 -04:00
|
|
|
|
replaced the `<ng-template>` with a diagnostic comment.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
Angular会在真正渲染的时候填充`<ng-template>`的内容,并且把`<ng-template>`替换为一个供诊断用的注释。
|
|
|
|
|
|
2017-03-11 10:36:40 -05:00
|
|
|
|
The [`NgFor`](guide/structural-directives#ngFor) and [`NgSwitch...`](guide/structural-directives#ngSwitch) directives follow the same pattern.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
[`NgFor`](guide/structural-directives#ngFor)和[`NgSwitch...`](guide/structural-directives#ngSwitch)指令也都遵循同样的模式。
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
{@a ngFor}
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
## Inside _*ngFor_
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
## `*ngFor`内幕
|
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
Angular transforms the `*ngFor` in similar fashion from asterisk (*) syntax through
|
2017-04-26 08:11:02 -04:00
|
|
|
|
template _attribute_ to `<ng-template>` _element_.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
Angular会把`*ngFor`用同样的方式把星号(*)语法的`template`*属性*转换成`<ng-template>`*元素*。
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
Here's a full-featured application of `NgFor`, written all three ways:
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
这里有一个`NgFor`的全特性应用,同时用了这三种写法:
|
|
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
|
|
<code-example path="structural-directives/src/app/app.component.html" linenums="false" title="src/app/app.component.html (inside-ngfor)" region="inside-ngfor">
|
|
|
|
|
|
|
|
|
|
</code-example>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
This is manifestly more complicated than `ngIf` and rightly so.
|
|
|
|
|
The `NgFor` directive has more features, both required and optional, than the `NgIf` shown in this guide.
|
|
|
|
|
At minimum `NgFor` needs a looping variable (`let hero`) and a list (`heroes`).
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
它明显比`ngIf`复杂得多,确实如此。
|
|
|
|
|
`NgFor`指令比本章展示过的`NgIf`具有更多的必选特性和可选特性。
|
|
|
|
|
至少`NgFor`会需要一个循环变量(`let hero`)和一个列表(`heroes`)。
|
|
|
|
|
|
2017-04-26 08:11:02 -04:00
|
|
|
|
You enable these features in the string assigned to `ngFor`, which you write in Angular's [microsyntax](guide/structural-directives#microsyntax).
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
我们可以通过把一个字符串赋值给`ngFor`来启用这些特性,这个字符串使用Angular的[微语法](guide/structural-directives#microsyntax)。
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-04-10 11:51:13 -04:00
|
|
|
|
<div class="alert is-helpful">
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-04-26 08:11:02 -04:00
|
|
|
|
Everything _outside_ the `ngFor` string stays with the host element
|
|
|
|
|
(the `<div>`) as it moves inside the `<ng-template>`.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
In this example, the `[ngClass]="odd"` stays on the `<div>`.
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
`ngFor`字符串*之外*的每一样东西都会留在宿主元素(`<div>`)上,也就是说它移到了`<ng-template>`内部。
|
|
|
|
|
在这个例子中,`[ngClass]="odd"`留在了`<div>`上。
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-04-10 11:51:13 -04:00
|
|
|
|
</div>
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{@a microsyntax}
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
### Microsyntax
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
### 微语法
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
The Angular microsyntax lets you configure a directive in a compact, friendly string.
|
2017-04-12 15:53:18 -04:00
|
|
|
|
The microsyntax parser translates that string into attributes on the `<ng-template>`:
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
Angular微语法能让我们通过简短的、友好的字符串来配置一个指令。
|
|
|
|
|
微语法解析器把这个字符串翻译成`<ng-template>`上的属性:
|
|
|
|
|
|
2017-04-26 08:11:02 -04:00
|
|
|
|
* The `let` keyword declares a [_template input variable_](guide/structural-directives#template-input-variable)
|
2017-02-22 13:09:39 -05:00
|
|
|
|
that you reference within the template. The input variables in this example are `hero`, `i`, and `odd`.
|
2017-04-26 08:11:02 -04:00
|
|
|
|
The parser translates `let hero`, `let i`, and `let odd` into variables named,
|
2017-02-22 13:09:39 -05:00
|
|
|
|
`let-hero`, `let-i`, and `let-odd`.
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
`let`关键字声明一个[模板输入变量](guide/structural-directives#template-input-variable),我们会在模板中引用它。本例子中,这个输入变量就是`hero`、`i`和`odd`。
|
|
|
|
|
解析器会把`let hero`、`let i`和`let odd`翻译成命名变量`let-hero`、`let-i`和`let-odd`。
|
|
|
|
|
|
2017-04-26 08:11:02 -04:00
|
|
|
|
* The microsyntax parser takes `of` and `trackby`, title-cases them (`of` -> `Of`, `trackBy` -> `TrackBy`),
|
2017-02-22 13:09:39 -05:00
|
|
|
|
and prefixes them with the directive's attribute name (`ngFor`), yielding the names `ngForOf` and `ngForTrackBy`.
|
|
|
|
|
Those are the names of two `NgFor` _input properties_ .
|
|
|
|
|
That's how the directive learns that the list is `heroes` and the track-by function is `trackById`.
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
微语法解析器接收`of`和`trackby`,把它们首字母大写(`of` -> `Of`, `trackBy` -> `TrackBy`),
|
|
|
|
|
并且给它们加上指令的属性名(`ngFor`)前缀,最终生成的名字是`ngForOf`和`ngForTrackBy`。
|
|
|
|
|
还有两个`NgFor`的*输入属性*,指令据此了解到列表是`heroes`,而track-by函数是`trackById`。
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
* As the `NgFor` directive loops through the list, it sets and resets properties of its own _context_ object.
|
|
|
|
|
These properties include `index` and `odd` and a special property named `$implicit`.
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
`NgFor`指令在列表上循环,每个循环中都会设置和重置它自己的*上下文*对象上的属性。
|
|
|
|
|
这些属性包括`index`和`odd`以及一个特殊的属性名`$implicit`(隐式变量)。
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
* The `let-i` and `let-odd` variables were defined as `let i=index` and `let odd=odd`.
|
|
|
|
|
Angular sets them to the current value of the context's `index` and `odd` properties.
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
`let-i`和`let-odd`变量是通过`let i=index`和`let odd=odd`来定义的。
|
|
|
|
|
Angular把它们设置为*上下文*对象中的`index`和`odd`属性的当前值。
|
|
|
|
|
|
2017-04-26 08:11:02 -04:00
|
|
|
|
* The context property for `let-hero` wasn't specified.
|
|
|
|
|
It's intended source is implicit.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
Angular sets `let-hero` to the value of the context's `$implicit` property
|
|
|
|
|
which `NgFor` has initialized with the hero for the current iteration.
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
上下文中的属性`let-hero`没有指定过,实际上它来自一个隐式变量。
|
|
|
|
|
Angular会把`let-hero`设置为上下文对象中的`$implicit`属性,`NgFor`会用当前迭代中的英雄初始化它。
|
|
|
|
|
|
2017-04-30 16:10:32 -04:00
|
|
|
|
* The [API guide](api/common/NgFor "API: NgFor")
|
2017-02-22 13:09:39 -05:00
|
|
|
|
describes additional `NgFor` directive properties and context properties.
|
|
|
|
|
|
2017-07-30 00:32:56 -04:00
|
|
|
|
[API参考手册](api/common/NgFor "API: NgFor")中描述了`NgFor`指令的其它属性和上下文属性。
|
2017-07-22 23:51:25 -04:00
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
These microsyntax mechanisms are available to you when you write your own structural directives.
|
2017-04-26 08:11:02 -04:00
|
|
|
|
Studying the
|
2017-04-12 15:53:18 -04:00
|
|
|
|
[source code for `NgIf`](https://github.com/angular/angular/blob/master/packages/common/src/directives/ng_if.ts "Source: NgIf")
|
2017-04-26 08:11:02 -04:00
|
|
|
|
and [`NgFor`](https://github.com/angular/angular/blob/master/packages/common/src/directives/ng_for_of.ts "Source: NgFor")
|
2017-04-12 15:53:18 -04:00
|
|
|
|
is a great way to learn more.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
这些微语法机制在你写自己的结构型指令时也同样有效,参考[`NgIf`的源码](https://github.com/angular/angular/blob/master/packages/common/src/directives/ng_if.ts "Source: NgIf")
|
|
|
|
|
和[`NgFor`的源码](https://github.com/angular/angular/blob/master/packages/common/src/directives/ng_for_of.ts "Source: NgFor") 可以学到更多。
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{@a template-input-variable}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{@a template-input-variables}
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
### Template input variable
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
### 模板输入变量
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
A _template input variable_ is a variable whose value you can reference _within_ a single instance of the template.
|
2017-03-11 08:44:25 -05:00
|
|
|
|
There are several such variables in this example: `hero`, `i`, and `odd`.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
All are preceded by the keyword `let`.
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
*模板输入变量*是这样一种变量,你可以*在单个实例的模板中*引用它的值。
|
|
|
|
|
这个例子中有好几个模板输入变量:`hero`、`i`和`odd`。
|
|
|
|
|
它们都是用`let`作为前导关键字。
|
|
|
|
|
|
2017-04-26 08:11:02 -04:00
|
|
|
|
A _template input variable_ is **_not_** the same as a
|
2017-03-31 19:57:13 -04:00
|
|
|
|
[template _reference_ variable](guide/template-syntax#ref-vars),
|
2017-02-22 13:09:39 -05:00
|
|
|
|
neither _semantically_ nor _syntactically_.
|
|
|
|
|
|
2017-04-26 08:11:02 -04:00
|
|
|
|
You declare a template _input_ variable using the `let` keyword (`let hero`).
|
2017-02-22 13:09:39 -05:00
|
|
|
|
The variable's scope is limited to a _single instance_ of the repeated template.
|
|
|
|
|
You can use the same variable name again in the definition of other structural directives.
|
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
You declare a template _reference_ variable by prefixing the variable name with `#` (`#var`).
|
2017-02-22 13:09:39 -05:00
|
|
|
|
A _reference_ variable refers to its attached element, component or directive.
|
|
|
|
|
It can be accessed _anywhere_ in the _entire template_.
|
|
|
|
|
|
|
|
|
|
Template _input_ and _reference_ variable names have their own namespaces. The `hero` in `let hero` is never the same
|
|
|
|
|
variable as the `hero` declared as `#hero`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{@a one-per-element}
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
### One structural directive per host element
|
|
|
|
|
|
2017-03-06 05:43:33 -05:00
|
|
|
|
Someday you'll want to repeat a block of HTML but only when a particular condition is true.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
You'll _try_ to put both an `*ngFor` and an `*ngIf` on the same host element.
|
2017-04-26 08:11:02 -04:00
|
|
|
|
Angular won't let you. You may apply only one _structural_ directive to an element.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
|
|
|
|
The reason is simplicity. Structural directives can do complex things with the host element and its descendents.
|
2017-04-26 08:11:02 -04:00
|
|
|
|
When two directives lay claim to the same host element, which one takes precedence?
|
2017-02-22 13:09:39 -05:00
|
|
|
|
Which should go first, the `NgIf` or the `NgFor`? Can the `NgIf` cancel the effect of the `NgFor`?
|
|
|
|
|
If so (and it seems like it should be so), how should Angular generalize the ability to cancel for other structural directives?
|
|
|
|
|
|
|
|
|
|
There are no easy answers to these questions. Prohibiting multiple structural directives makes them moot.
|
|
|
|
|
There's an easy solution for this use case: put the `*ngIf` on a container element that wraps the `*ngFor` element.
|
2017-03-11 10:36:40 -05:00
|
|
|
|
One or both elements can be an [`ng-container`](guide/structural-directives#ngcontainer) so you don't have to introduce extra levels of HTML.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
{@a ngSwitch}
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
## Inside _NgSwitch_ directives
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
|
|
|
|
The Angular _NgSwitch_ is actually a set of cooperating directives: `NgSwitch`, `NgSwitchCase`, and `NgSwitchDefault`.
|
|
|
|
|
|
|
|
|
|
Here's an example.
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<code-example path="structural-directives/src/app/app.component.html" linenums="false" title="src/app/app.component.html (ngswitch)" region="ngswitch">
|
|
|
|
|
|
|
|
|
|
</code-example>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
The switch value assigned to `NgSwitch` (`hero.emotion`) determines which
|
|
|
|
|
(if any) of the switch cases are displayed.
|
|
|
|
|
|
2017-04-26 08:11:02 -04:00
|
|
|
|
`NgSwitch` itself is not a structural directive.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
It's an _attribute_ directive that controls the behavior of the other two switch directives.
|
|
|
|
|
That's why you write `[ngSwitch]`, never `*ngSwitch`.
|
|
|
|
|
|
2017-04-26 08:11:02 -04:00
|
|
|
|
`NgSwitchCase` and `NgSwitchDefault` _are_ structural directives.
|
|
|
|
|
You attach them to elements using the asterisk (*) prefix notation.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
An `NgSwitchCase` displays its host element when its value matches the switch value.
|
|
|
|
|
The `NgSwitchDefault` displays its host element when no sibling `NgSwitchCase` matches the switch value.
|
|
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
2017-04-10 11:51:13 -04:00
|
|
|
|
<div class="l-sub-section">
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-07-29 12:03:22 -04:00
|
|
|
|
*Design thought*: minimize initialization effort and consider caching state in a
|
2017-07-22 23:51:25 -04:00
|
|
|
|
companion service.
|
2017-07-30 00:32:56 -04:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
*设计思路*:要最小化初始化的成本,并考虑把状态缓存在一个伴生的服务中。
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
|
|
|
2017-04-10 11:51:13 -04:00
|
|
|
|
</div>
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-04-26 08:11:02 -04:00
|
|
|
|
As with other structural directives, the `NgSwitchCase` and `NgSwitchDefault`
|
2017-03-11 08:44:25 -05:00
|
|
|
|
can be desugared into the template _attribute_ form.
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
2017-07-22 23:51:25 -04: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.
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
**同样的考量也适用于每一个结构型指令,无论是内置的还是自定义的。**
|
|
|
|
|
我们应该提醒自己以及我们指令的使用者,来仔细考虑添加元素、移除元素以及创建和销毁组件的后果。
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
Let's see these dynamics at work. For fun, we'll stack the deck *against*
|
|
|
|
|
our recommendation and consider a component called `heavy-loader` that
|
|
|
|
|
***pretends*** to load a ton of data when initialized.
|
|
|
|
|
|
|
|
|
|
让我们在实践中看看这些变化。为了娱乐,我们设想在甲板上有个叫`heavy-loader`(重型起重机)的组件,它会***假装***在初始化时装载一吨数据。
|
|
|
|
|
|
|
|
|
|
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`.
|
|
|
|
|
|
|
|
|
|
我们将显示该组件的两个实例。我们使用CSS切换第一个实例的可见性,用`ngIf`把第二个实例添加到DOM和将其移除。
|
|
|
|
|
|
|
|
|
|
|
2017-04-12 15:53:18 -04:00
|
|
|
|
That, in turn, can be desugared into the `<ng-template>` element form.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
|
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
|
<code-example path="structural-directives/src/app/app.component.html" linenums="false" title="src/app/app.component.html (ngswitch-template)" region="ngswitch-template">
|
|
|
|
|
|
|
|
|
|
</code-example>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
{@a prefer-asterisk}
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
## Prefer the asterisk (*) syntax.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
The asterisk (*) syntax is more clear than the other desugared forms.
|
2017-03-11 10:36:40 -05:00
|
|
|
|
Use [<ng-container>](guide/structural-directives#ng-container) when there's no single element
|
2017-02-22 13:09:39 -05:00
|
|
|
|
to host the directive.
|
|
|
|
|
|
|
|
|
|
While there's rarely a good reason to apply a structural directive in template _attribute_ or _element_ form,
|
2017-04-12 15:53:18 -04:00
|
|
|
|
it's still important to know that Angular creates a `<ng-template>` and to understand how it works.
|
|
|
|
|
You'll refer to the `<ng-template>` when you [write your own structural directive](guide/structural-directives#unless).
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{@a template}
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-04-12 15:53:18 -04:00
|
|
|
|
## The *<ng-template>*
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-04-12 15:53:18 -04:00
|
|
|
|
The <ng-template> is an Angular element for rendering HTML.
|
2017-04-26 08:11:02 -04:00
|
|
|
|
It is never displayed directly.
|
2017-04-12 15:53:18 -04:00
|
|
|
|
In fact, before rendering the view, Angular _replaces_ the `<ng-template>` and its contents with a comment.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-04-12 15:53:18 -04:00
|
|
|
|
If there is no structural directive and you merely wrap some elements in a `<ng-template>`,
|
2017-02-22 13:09:39 -05:00
|
|
|
|
those elements disappear.
|
2017-03-11 08:44:25 -05:00
|
|
|
|
That's the fate of the middle "Hip!" in the phrase "Hip! Hip! Hooray!".
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<code-example path="structural-directives/src/app/app.component.html" linenums="false" title="src/app/app.component.html (template-tag)" region="template-tag">
|
|
|
|
|
|
|
|
|
|
</code-example>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
Angular erases the middle "Hip!", leaving the cheer a bit less enthusiastic.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
借助内置的`ngOnInit`和`ngOnDestroy`[生命周期钩子](guide/lifecycle-hooks),我们同时记录了组件的创建或销毁过程。
|
|
|
|
|
下面是它的操作演示:
|
|
|
|
|
|
2017-03-30 15:04:18 -04:00
|
|
|
|
|
2017-05-09 18:53:32 -04:00
|
|
|
|
<figure>
|
2017-05-11 15:44:22 -04:00
|
|
|
|
<img src='generated/images/guide/structural-directives/template-rendering.png' alt="template tag rendering">
|
2017-02-22 13:09:39 -05:00
|
|
|
|
</figure>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-04-12 15:53:18 -04:00
|
|
|
|
A structural directive puts a `<ng-template>` to work
|
2017-03-11 10:36:40 -05:00
|
|
|
|
as you'll see when you [write your own structural directive](guide/structural-directives#unless).
|
2017-03-11 08:44:25 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{@a ngcontainer}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{@a ng-container}
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
## Group sibling elements with <ng-container>
|
|
|
|
|
|
|
|
|
|
There's often a _root_ element that can and should host the structural directive.
|
|
|
|
|
The list element (`<li>`) is a typical host element of an `NgFor` repeater.
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<code-example path="structural-directives/src/app/app.component.html" linenums="false" title="src/app/app.component.html (ngfor-li)" region="ngfor-li">
|
|
|
|
|
|
|
|
|
|
</code-example>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
When there isn't a host element, you can usually wrap the content in a native HTML container element,
|
|
|
|
|
such as a `<div>`, and attach the directive to that wrapper.
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<code-example path="structural-directives/src/app/app.component.html" linenums="false" title="src/app/app.component.html (ngif)" region="ngif">
|
|
|
|
|
|
|
|
|
|
</code-example>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-04-26 08:11:02 -04:00
|
|
|
|
Introducing another container element—typically a `<span>` or `<div>`—to
|
|
|
|
|
group the elements under a single _root_ is usually harmless.
|
2017-03-11 08:44:25 -05:00
|
|
|
|
_Usually_ ... but not _always_.
|
|
|
|
|
|
2017-04-26 08:11:02 -04:00
|
|
|
|
The grouping element may break the template appearance because CSS styles
|
2017-03-11 08:44:25 -05:00
|
|
|
|
neither expect nor accommodate the new layout.
|
|
|
|
|
For example, suppose you have the following paragraph layout.
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<code-example path="structural-directives/src/app/app.component.html" linenums="false" title="src/app/app.component.html (ngif-span)" region="ngif-span">
|
|
|
|
|
|
|
|
|
|
</code-example>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
You also have a CSS style rule that happens to apply to a `<span>` within a `<p>`aragraph.
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<code-example path="structural-directives/src/app/app.component.css" linenums="false" title="src/app/app.component.css (p-span)" region="p-span">
|
|
|
|
|
|
|
|
|
|
</code-example>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
The constructed paragraph renders strangely.
|
|
|
|
|
|
2017-03-30 15:04:18 -04:00
|
|
|
|
|
2017-05-09 18:53:32 -04:00
|
|
|
|
<figure>
|
|
|
|
|
<img src='generated/images/guide/structural-directives/bad-paragraph.png' alt="spanned paragraph with bad style">
|
2017-03-11 08:44:25 -05:00
|
|
|
|
</figure>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
The `p span` style, intended for use elsewhere, was inadvertently applied here.
|
|
|
|
|
|
|
|
|
|
Another problem: some HTML elements require all immediate children to be of a specific type.
|
2017-04-26 08:11:02 -04:00
|
|
|
|
For example, the `<select>` element requires `<option>` children.
|
2017-03-11 08:44:25 -05:00
|
|
|
|
You can't wrap the _options_ in a conditional `<div>` or a `<span>`.
|
|
|
|
|
|
|
|
|
|
When you try this,
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<code-example path="structural-directives/src/app/app.component.html" linenums="false" title="src/app/app.component.html (select-span)" region="select-span">
|
|
|
|
|
|
|
|
|
|
</code-example>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
the drop down is empty.
|
|
|
|
|
|
2017-03-30 15:04:18 -04:00
|
|
|
|
|
2017-05-09 18:53:32 -04:00
|
|
|
|
<figure>
|
|
|
|
|
<img src='generated/images/guide/structural-directives/bad-select.png' alt="spanned options don't work">
|
2017-03-11 08:44:25 -05:00
|
|
|
|
</figure>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
The browser won't display an `<option>` within a `<span>`.
|
|
|
|
|
|
|
|
|
|
### <ng-container> to the rescue
|
|
|
|
|
|
|
|
|
|
The Angular `<ng-container>` is a grouping element that doesn't interfere with styles or layout
|
2017-04-26 08:11:02 -04:00
|
|
|
|
because Angular _doesn't put it in the DOM_.
|
2017-03-11 08:44:25 -05:00
|
|
|
|
|
|
|
|
|
Here's the conditional paragraph again, this time using `<ng-container>`.
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<code-example path="structural-directives/src/app/app.component.html" linenums="false" title="src/app/app.component.html (ngif-ngcontainer)" region="ngif-ngcontainer">
|
|
|
|
|
|
|
|
|
|
</code-example>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
It renders properly.
|
|
|
|
|
|
2017-03-30 15:04:18 -04:00
|
|
|
|
|
2017-05-09 18:53:32 -04:00
|
|
|
|
<figure>
|
|
|
|
|
<img src='generated/images/guide/structural-directives/good-paragraph.png' alt="ngcontainer paragraph with proper style">
|
2017-03-11 08:44:25 -05:00
|
|
|
|
</figure>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
Now conditionally exclude a _select_ `<option>` with `<ng-container>`.
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<code-example path="structural-directives/src/app/app.component.html" linenums="false" title="src/app/app.component.html (select-ngcontainer)" region="select-ngcontainer">
|
|
|
|
|
|
|
|
|
|
</code-example>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
The drop down works properly.
|
|
|
|
|
|
2017-03-30 15:04:18 -04:00
|
|
|
|
|
2017-05-09 18:53:32 -04:00
|
|
|
|
<figure>
|
|
|
|
|
<img src='generated/images/guide/structural-directives/select-ngcontainer-anim.gif' alt="ngcontainer options work properly">
|
2017-03-11 08:44:25 -05:00
|
|
|
|
</figure>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
The `<ng-container>` is a syntax element recognized by the Angular parser.
|
2017-04-26 08:11:02 -04:00
|
|
|
|
It's not a directive, component, class, or interface.
|
2017-03-11 08:44:25 -05:00
|
|
|
|
It's more like the curly braces in a JavaScript `if`-block:
|
|
|
|
|
|
2017-03-30 15:04:18 -04:00
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
<code-example language="javascript">
|
2017-03-31 19:57:13 -04:00
|
|
|
|
if (someCondition) {
|
|
|
|
|
statement1;
|
|
|
|
|
statement2;
|
|
|
|
|
statement3;
|
2017-04-26 08:11:02 -04:00
|
|
|
|
}
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
</code-example>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
Without those braces, JavaScript would only execute the first statement
|
|
|
|
|
when you intend to conditionally execute all of them as a single block.
|
|
|
|
|
The `<ng-container>` satisfies a similar need in Angular templates.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{@a unless}
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
## Write a structural directive
|
2017-03-11 08:44:25 -05:00
|
|
|
|
|
|
|
|
|
In this section, you write an `UnlessDirective` structural directive
|
2017-02-22 13:09:39 -05:00
|
|
|
|
that does the opposite of `NgIf`.
|
|
|
|
|
`NgIf` displays the template content when the condition is `true`.
|
|
|
|
|
`UnlessDirective` displays the content when the condition is ***false***.
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<code-example path="structural-directives/src/app/app.component.html" linenums="false" title="src/app/app.component.html (myUnless-1)" region="myUnless-1">
|
|
|
|
|
|
|
|
|
|
</code-example>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
Creating a directive is similar to creating a component.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
创建指令很像创建组件。
|
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
* Import the `Directive` decorator (instead of the `Component` decorator).
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
导入`Directive`装饰器(而不再是`Component`)。
|
|
|
|
|
|
|
|
|
|
* Import the `Input`, `TemplateRef`, and `ViewContainerRef` symbols; you'll need them for _any_ structural directive .
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
* Apply the decorator to the directive class.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
* Set the CSS *attribute selector* that identifies the directive when applied to an element in a template.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
Here's how you might begin:
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<code-example path="structural-directives/src/app/unless.directive.ts" linenums="false" title="src/app/unless.directive.ts (skeleton)" region="skeleton">
|
|
|
|
|
|
|
|
|
|
</code-example>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
The directive's _selector_ is typically the directive's **attribute name** in square brackets, `[myUnless]`.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
The brackets define a CSS
|
2017-04-24 14:23:45 -04:00
|
|
|
|
<a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors" title="MDN: Attribute selectors">attribute selector</a>.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
|
|
|
|
The directive _attribute name_ should be spelled in _lowerCamelCase_ and begin with a prefix.
|
|
|
|
|
Don't use `ng`. That prefix belongs to Angular.
|
|
|
|
|
Pick something short that fits you or your company.
|
|
|
|
|
In this example, the prefix is `my`.
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-04-21 20:21:45 -04:00
|
|
|
|
The directive _class_ name ends in `Directive` per the [style guide](guide/styleguide#02-03 "Angular Style Guide").
|
2017-02-22 13:09:39 -05:00
|
|
|
|
Angular's own directives do not.
|
|
|
|
|
|
|
|
|
|
### _TemplateRef_ and _ViewContainerRef_
|
|
|
|
|
|
2017-04-26 08:11:02 -04:00
|
|
|
|
A simple structural directive like this one creates an
|
2017-04-30 16:10:32 -04:00
|
|
|
|
[_embedded view_](api/core/EmbeddedViewRef "API: EmbeddedViewRef")
|
2017-04-26 08:11:02 -04:00
|
|
|
|
from the Angular-generated `<ng-template>` and inserts that view in a
|
2017-04-30 16:10:32 -04:00
|
|
|
|
[_view container_](api/core/ViewContainerRef "API: ViewContainerRef")
|
2017-02-22 13:09:39 -05:00
|
|
|
|
adjacent to the directive's original `<p>` host element.
|
|
|
|
|
|
2017-04-12 15:53:18 -04:00
|
|
|
|
You'll acquire the `<ng-template>` contents with a
|
2017-04-30 16:10:32 -04:00
|
|
|
|
[`TemplateRef`](api/core/TemplateRef "API: TemplateRef")
|
2017-02-22 13:09:39 -05:00
|
|
|
|
and access the _view container_ through a
|
2017-04-30 16:10:32 -04:00
|
|
|
|
[`ViewContainerRef`](api/core/ViewContainerRef "API: ViewContainerRef").
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
|
|
|
|
You inject both in the directive constructor as private variables of the class.
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<code-example path="structural-directives/src/app/unless.directive.ts" linenums="false" title="src/app/unless.directive.ts (ctor)" region="ctor">
|
|
|
|
|
|
|
|
|
|
</code-example>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
### The _myUnless_ property
|
|
|
|
|
|
|
|
|
|
The directive consumer expects to bind a true/false condition to `[myUnless]`.
|
2017-04-26 08:11:02 -04:00
|
|
|
|
That means the directive needs a `myUnless` property, decorated with `@Input`
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
2017-04-10 11:51:13 -04:00
|
|
|
|
<div class="l-sub-section">
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Read about `@Input` in the [_Template Syntax_](guide/template-syntax#inputs-outputs) guide.
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
|
|
|
2017-04-10 11:51:13 -04:00
|
|
|
|
</div>
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<code-example path="structural-directives/src/app/unless.directive.ts" linenums="false" title="src/app/unless.directive.ts (set)" region="set">
|
|
|
|
|
|
|
|
|
|
</code-example>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
Angular sets the `myUnless` property whenever the value of the condition changes.
|
|
|
|
|
Because the `myUnless` property does work, it needs a setter.
|
|
|
|
|
|
|
|
|
|
* If the condition is falsy and the view hasn't been created previously,
|
|
|
|
|
tell the _view container_ to create the _embedded view_ from the template.
|
|
|
|
|
|
2017-04-26 08:11:02 -04:00
|
|
|
|
* If the condition is truthy and the view is currently displayed,
|
2017-02-22 13:09:39 -05:00
|
|
|
|
clear the container which also destroys the view.
|
|
|
|
|
|
|
|
|
|
Nobody reads the `myUnless` property so it doesn't need a getter.
|
|
|
|
|
|
|
|
|
|
The completed directive code looks like this:
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<code-example path="structural-directives/src/app/unless.directive.ts" linenums="false" title="src/app/unless.directive.ts (excerpt)" region="no-docs">
|
|
|
|
|
|
|
|
|
|
</code-example>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-03-31 07:23:16 -04:00
|
|
|
|
Add this directive to the `declarations` array of the AppModule.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
|
|
|
|
Then create some HTML to try it.
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<code-example path="structural-directives/src/app/app.component.html" linenums="false" title="src/app/app.component.html (myUnless)" region="myUnless">
|
|
|
|
|
|
|
|
|
|
</code-example>
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
When the `condition` is falsy, the top (A) paragraph appears and the bottom (B) paragraph disappears.
|
|
|
|
|
When the`condition` is truthy, the top (A)paragraph is removed and the bottom (B)paragraph appears.
|
|
|
|
|
|
|
|
|
|
当`condition`为`false`时,顶部的段落就会显示出来,而底部的段落消失了。
|
|
|
|
|
当`condition`为`true`时,顶部的段落被移除了,而底部的段落显示了出来。
|
2017-03-11 08:44:25 -05:00
|
|
|
|
|
2017-03-30 15:04:18 -04:00
|
|
|
|
|
2017-05-09 18:53:32 -04:00
|
|
|
|
<figure>
|
|
|
|
|
<img src='generated/images/guide/structural-directives/unless-anim.gif' alt="UnlessDirective in action">
|
2017-02-22 13:09:39 -05:00
|
|
|
|
</figure>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{@a summary}
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
## Summary
|
2017-03-11 08:44:25 -05:00
|
|
|
|
|
2017-04-26 08:11:02 -04:00
|
|
|
|
You can both try and download the source code for this guide in the <live-example></live-example>.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-03-11 08:44:25 -05:00
|
|
|
|
Here is the source from the `src/app/` folder.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-07-22 23:51:25 -04:00
|
|
|
|
本章相关的代码如下:
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
|
<code-tabs>
|
|
|
|
|
|
|
|
|
|
<code-pane title="app.component.ts" path="structural-directives/src/app/app.component.ts">
|
|
|
|
|
|
|
|
|
|
</code-pane>
|
|
|
|
|
|
|
|
|
|
<code-pane title="app.component.html" path="structural-directives/src/app/app.component.html">
|
|
|
|
|
|
|
|
|
|
</code-pane>
|
|
|
|
|
|
|
|
|
|
<code-pane title="app.component.css" path="structural-directives/src/app/app.component.css">
|
|
|
|
|
|
|
|
|
|
</code-pane>
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
|
<code-pane title="app.module.ts" path="structural-directives/src/app/app.module.ts">
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
|
</code-pane>
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
|
<code-pane title="hero.ts" path="structural-directives/src/app/hero.ts">
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
|
</code-pane>
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
|
<code-pane title="hero-switch.components.ts" path="structural-directives/src/app/hero-switch.components.ts">
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
|
</code-pane>
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
|
<code-pane title="unless.directive.ts" path="structural-directives/src/app/unless.directive.ts">
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
|
</code-pane>
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
|
</code-tabs>
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
You learned
|
2017-03-11 08:44:25 -05:00
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
|
* that structural directives manipulate HTML layout.
|
2017-03-11 10:36:40 -05:00
|
|
|
|
* to use [`<ng-container>`](guide/structural-directives#ngcontainer) as a grouping element when there is no suitable host element.
|
2017-04-12 15:53:18 -04:00
|
|
|
|
* that the Angular desugars [asterisk (*) syntax](guide/structural-directives#asterisk) into a `<ng-template>`.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
* how that works for the `NgIf`, `NgFor` and `NgSwitch` built-in directives.
|
2017-04-12 15:53:18 -04:00
|
|
|
|
* about the [_microsyntax_](guide/structural-directives#microsyntax) that expands into a [`<ng-template>`](guide/structural-directives#template).
|
2017-04-24 14:23:45 -04:00
|
|
|
|
* to write a [custom structural directive](guide/structural-directives#unless), `UnlessDirective`.
|