Angular2 how to set element class name when using ng-for, only on first element

As requested by @jeff

You can achieve by simply using this line

<li *ngFor="let tab of tabs; let index = index" [class.active]="index == 0" ...>

Glad it helped 🙂

Update

With beta 15 the first local variable was added, so the original solution can be rewritten as

<li *ngFor="let tab of tabs; let isFirst = first" [class.active]="isFirst" ...>

See Angular 2 – ngFor – local variable “first” does not work

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)