more_horiz
@angular/commoncode
NgClass
directive
stable
Adds and removes CSS classes on an HTML element.
Class bindingsDescription
Adds and removes CSS classes on an HTML element.
The CSS classes are updated as follows, depending on the type of the expression evaluation:
string- the CSS classes listed in the string (space delimited) are added,Array- the CSS classes declared as Array elements are added,Object- keys are CSS classes that get added when the expression given in the value evaluates to a truthy value, otherwise they are removed.
Exported by
Usage Notes
<some-element [ngClass]="stringExp|arrayExp|objExp|Set">...</some-element>
<some-element [ngClass]="{'class1 class2 class3' : true}">...</some-element>
For more simple use cases you can use the class bindings directly. It doesn't require importing a directive.
<some-element [class]="'first second'">...</some-element>
<some-element [class.expanded]="isExpanded">...</some-element>
<some-element [class]="['first', 'second']">...</some-element>
<some-element [class]="{'first': true, 'second': true, 'third': false}">...</some-element>
Jump to details