angularstylingng-classangular-ng-class

How to avoid repetitions within Angular ngClass?


Imagine following situation:

 <div *ngFor="let d of dm; first as first">
    <span
      [ngClass]="{ cSmall: !first, cGray: !first, cTab1: !first }"
    >
      {{ d }}
    </span>
  </div>

All these CSS-Classes cSmall, cGray, cTab1 are depended on !first.

Can I avoid unnecessary repetitions within:

{ cSmall: !first, cGray: !first, cTab1: !first }

I've alredy tried { [cSmall,cGray,cTab1]: !first } with no success :)


Solution

  • Try

    <some-element [ngClass]="{'class1 class2 class3' : true}">...</some-element>
    

    Reference : NgClass API