Adds and removes CSS classes on an HTML element.
API
class NgClass implements DoCheck {
constructor(_ngEl: ElementRef<any>, _renderer: Renderer2): NgClass;
@Input('class') set klass(value: string);
@Input() set ngClass(value: string | string[] | Set<string> | { [klass: string]: any; } | null | undefined);
}
constructor
NgClassklass
stringngClass
string | string[] | Set<string> | { [klass: string]: any; } | null | undefinedngDoCheck
void
@returns
void
Description
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>