简体   繁体   中英

Angular: use a class with *ngIf

<ul class="list-group">
  <li *ngFor="let t of todos; let i = index" class="list-group-item" >{{ t }}</li>
</ul>

t is a string. I want to have this element if i == 0 :

<li class="list-group-item active" >{{ t }}</li>

and this element if i > 0 :

<li class="list-group-item" >{{ t }}</li>

您可以尝试以下方法:

<li *ngFor="let t of todos; let i = index" class="list-group-item" [class.active]="i==0" >{{ t }}</li>

I like to use [class.####] notation for this sort of thing.

 <li *ngFor="let t of todos; let i = index" 
     class="list-group-item"
     [class.active]="i === 0">{{ t }}</li>

You can also use first.

 <li *ngFor="let t of todos; let i = first" 
     class="list-group-item"
     [class.active]="i">{{ t }}</li>
<ul class="list-group">
    <li *ngFor="let t of todos; let i = index" class="list-group-item" >
        <span [class.active]="i==0"> {{ t }} </span>  
    </li>
</ul>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM