Skip to content

Instantly share code, notes, and snippets.

@vxhviet
Last active November 12, 2018 08:26
Show Gist options
  • Save vxhviet/9eaa0f195a7fb5d28c9d7e32eddafe24 to your computer and use it in GitHub Desktop.
Save vxhviet/9eaa0f195a7fb5d28c9d7e32eddafe24 to your computer and use it in GitHub Desktop.

Angular - Add Custom Class to Element With ngClass

SOURCE, SOURCE

Another syntax for ngClass:

add class hidden-element to div if the condition is satisfied

<div [class.hidden-element]="item.hidden">
  ...
</div>

this is similar to

<some-element [ngClass]="{'first': true, 'second': true, 'third': false}">...</some-element>

this will add a CSS class named first to the first element of the list, and a CSS class named last to the last element of the list:

<tr *ngFor="let hero of heroes; let first = first; let last = last" 
    [ngClass]="{ first: first, last: last }">
    <td>{{hero.name}}</td>
</tr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment