在 Angular 中最經常使用的指令分爲兩種,它們分別是 屬性型指令 和 結構型指令。html
<div class="box" *ngIf="false">看不見我</div>
<!-- isSpecial is true --> <div [class.hidden]="!isSpecial">Show with class</div> <div [class.hidden]="isSpecial">Hide with class</div> <div [style.display]="isSpecial ? 'block' : 'none'">Show with style</div> <div [style.display]="isSpecial ? 'none' : 'block'">Hide with style</div>
NgSwitch
、NgSwitchCase
、NgSwitchDefault
<div [ngSwitch]="currentHero.emotion"> <app-happy-hero *ngSwitchCase="'happy'" [hero]="currentHero"></app-happy-hero> <app-sad-hero *ngSwitchCase="'sad'" [hero]="currentHero"></app-sad-hero> <app-confused-hero *ngSwitchCase="'confused'" [hero]="currentHero"></app-confused-hero> <app-unknown-hero *ngSwitchDefault [hero]="currentHero"></app-unknown-hero> </div>
<div *ngFor="let hero of heroes">{{hero.name}}</div>
*ngFor
<ul> <li *ngFor="let item of todos; let i = index">{{ item.title + i }}</li> </ul>
參考文檔:ide