Skip to content

Instantly share code, notes, and snippets.

@riscie
Created June 2, 2020 13:37
Show Gist options
  • Save riscie/f5557bc2d479ba87d0edf5b50e96d0ac to your computer and use it in GitHub Desktop.
Save riscie/f5557bc2d479ba87d0edf5b50e96d0ac to your computer and use it in GitHub Desktop.
angular [ngClass] #angular

Angular version 2,...,9 provides several ways to add classes conditionally:

type one

[class.my-class]="step === 'step1'"

type two

[ngClass]="{'my-class': step === 'step1'}"

and multiple option:

[ngClass]="{'my-class': step === 'step1', 'my-class2':step === 'step2' }"

type three

[ngClass]="{1:'my-class1',2:'my-class2',3:'my-class4'}[step]"

type four

[ngClass]="(step=='step1')?'my-class1':'my-class2'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment