block includes include ../_util-fns //- The docs standard h4 style uppercases, making code terms unreadable. Override it. style. h4 {font-size: 17px !important; text-transform: none !important;} .syntax { font-family: Consolas, 'Lucida Sans', Courier, sans-serif; color: black; font-size: 85%; } :marked This guide looks at how Angular manipulates the DOM with **structural directives** and how you can write your own structural directives to do the same thing. 在本章中,我们将看看Angular如何操纵DOM树,以及我们该如何在自己的指令中这么做。 ### Table of contents ### 目录 * [What are structural directives?](#definition) [什么是结构型指令?](#definition) * [*NgIf* case study](#ngIf) [*NgIf* 案例](#ngIf) * [The asterisk (*) prefix](#asterisk) [星号(*)前缀](#asterisk) * [Inside *NgFor*](#ngFor) [*NgFor* 指令内幕](#ngFor) * [microsyntax](#microsyntax) [微语法](#microsyntax) * [template input variables](#template-input-variable) [模板输入变量](#template-input-variable) * [one structural directive per element](#one-per-element) [每个元素一个结构型指令](#one-per-element) * [Inside the *NgSwitch* directives](#ngSwitch) [*NgSwitch* 指令内幕](#ngSwitch) * [Prefer the (*) prefix](#prefer-asterisk) [优先使用(*)前缀](#prefer-asterisk) * [The <ng-template> element](#template) [<ng-template> 元素](#template) * [Group sibling elements with <ng-container>](#ng-container) [使用<ng-container>对兄弟元素进行分组](#ng-container) * [Write a structural directive](#unless) [写自己的结构型指令](#unless) Try the . 试试在线例子。 a#definition .l-main-section :marked ## What are structural directives? ## 什么是结构型指令? Structural directives are responsible for HTML layout. They shape or reshape the DOM's _structure_, typically by adding, removing, or manipulating elements. 结构型指令的职责是HTML布局。 它们塑造或重塑DOM的结构,比如添加、移除或维护这些元素。 As with other directives, you apply a structural directive to a _host element_. The directive then does whatever it's supposed to do with that host element and its descendents. 像其它指令一样,你可以把结构型指令应用到一个*宿主元素*上。 然后它就可以对宿主元素及其子元素做点什么。 Structural directives are easy to recognize. An asterisk (*) precedes the directive attribute name as in this example. 结构型指令非常容易识别。 在这个例子中,星号(*)被放在指令的属性名之前。 +makeExcerpt('src/app/app.component.html', 'ngif', '') :marked No brackets. No parentheses. Just `*ngIf` set to a string. 没有方括号,没有圆括号,只是把`*ngIf`设置为一个字符串。 You'll learn in this guide that the [asterisk (*) is a convenience notation](#asterisk) and the string isa [_microsyntax_](#microsyntax) rather than the usual [template expression](template-syntax.html#template-expressions). Angular desugars this notation into a marked-up `` that surrounds the host element and its descendents. Each structural directive does something different with that template. 在这个例子中,我们将学到[星号(*)这个简写方法](#asterisk),而这个字符串是一个[*微语法*](#microsyntax),而不是通常的[模板表达式](template-syntax.html#template-expressions)。 Angular会解开这个语法糖,变成一个``标记,包裹着宿主元素及其子元素。 每个结构型指令都可以用这个模板做点不同的事情。 Three of the common, built-in structural directives—[NgIf](template-syntax.html#ngIf), [NgFor](template-syntax.html#ngFor), and [NgSwitch...](template-syntax.html#ngSwitch)—are described in the [_Template Syntax_](template-syntax.html) guide and seen in samples throughout the Angular documentation. Here's an example of them in a template: 三个常用的内置结构型指令 —— [NgIf](template-syntax.html#ngIf)、[NgFor](template-syntax.html#ngFor)和[NgSwitch...](template-syntax.html#ngSwitch)。 我们在[*模板语法*](template-syntax.html)一章中讲过它,并且在Angular文档的例子中到处都在用它。下面是模板中的例子: +makeExcerpt('src/app/app.component.html', 'built-in', '') :marked This guide won't repeat how to _use_ them. But it does explain _how they work_ and how to [write your own](#unless) structural directive. .callout.is-helpful header Directive spelling :marked Throughout this guide, you'll see a directive spelled in both _UpperCamelCase_ and _lowerCamelCase_. Already you've seen `NgIf` and `ngIf`. There's a reason. `NgIf` refers to the directive _class_; `ngIf` refers to the directive's _attribute name_. 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. .l-sub-section :marked There are two other kinds of Angular directives, described extensively elsewhere: (1) components and (2) attribute directives. A *component* manages a region of HTML in the manner of a native HTML element. Technically it's a directive with a template. An [*attribute* directive](attribute-directives.html) changes the appearance or behavior of an element, component, or another directive. For example, the built-in [`NgStyle`](template-syntax.html#ngStyle) directive changes several element styles at the same time. You can apply many _attribute_ directives to one host element. You can [only apply one](#one-per-element) _structural_ directive to a host element. a#ngIf .l-main-section :marked ## NgIf case study ## NgIf案例分析 `NgIf` is the simplest structural directive and the easiest to understand. It takes a boolean expression and makes an entire chunk of the DOM appear or disappear. 我们重点看下`ngIf`。它是一个很好的结构型指令案例:它接受一个布尔值,并据此让一整块DOM树出现或消失。 +makeExcerpt('src/app/app.component.html', 'ngif-true', '') :marked 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. `ngIf`指令并不会隐藏元素。 使用浏览器的开发者工具就会看到:当`condition`为真的时候,只剩下了DOM顶部的段落,而底部无用的段落完全从DOM中消失了! 在它的位置上是空白的`