在 Angular 中最常用的指令分为两种,它们分别是 属性型指令 和 结构型指令。
<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>
参考文档:
原文:https://www.cnblogs.com/ygjzs/p/12228096.html