ng-指令

时间:2020-01-22 11:17:50   收藏:0   阅读:86

在 Angular 中最常用的指令分为两种,它们分别是 属性型指令结构型指令

NgClass

NgStyle

NgModel

NgIf

<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

<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>

NgFor

<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

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!