# 常用指令
# ngFor 指令
<div *ngFor="let item of items; let i=index">{{i + 1}} - {{item.name}}</div>
<li *ngFor="let user of users; index as i; first as isFirst">
{{i}}/{{users.length}}. {{user}} <span *ngIf="isFirst">default</span>
</li>
<ng-template ngFor let-item [ngForOf]="items" let-i="index" [ngForTrackBy]="trackByFn">
<li>...</li>
</ng-template>
<!--
NgForOf 导出了一系列值,可以指定别名后作为局部变量使用:
$implicit: T:迭代目标(绑定到 ngForOf)中每个条目的值。
ngForOf: NgIterable<T>:迭代表达式的值。当表达式不局限于访问某个属性时,这会非常有用,比如在使用 async 管道时(userStreams | async)。
index: number:可迭代对象中当前条目的索引。
count: number:可迭代对象的长度。
first: boolean:如果当前条目是可迭代对象中的第一个条目则为 true。
last: boolean:如果当前条目是可迭代对象中的最后一个条目则为 true。
even: boolean:如果当前条目在可迭代对象中的索引号为偶数则为 true。
odd: boolean:如果当前条目在可迭代对象中的索引号为奇数则为 true。
-->
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# NgSwitch 指令
<div [ngSwitch]="currentItem.feature">
<app-stout-item *ngSwitchCase="'stout'" [item]="currentItem"></app-stout-item>
<app-device-item *ngSwitchCase="'slim'" [item]="currentItem"></app-device-item>
<app-lost-item *ngSwitchCase="'vintage'" [item]="currentItem"></app-lost-item>
<app-best-item *ngSwitchCase="'bright'" [item]="currentItem"></app-best-item>
<app-unknown-item *ngSwitchDefault [item]="currentItem"></app-unknown-item>
</div>
1
2
3
4
5
6
7
2
3
4
5
6
7