The NgClass directive conditionally adds and removes CSS classes on an HTML element based on an expression's evaluation result.
The result of an expression evaluation is interpreted differently depending on type of the expression evaluation result:
-
String
- all the CSS classes listed in a string (space delimited) are added List
- all the CSS classes (List elements) are added-
Object
- each key corresponds to a CSS class name while values are interpreted as expressions evaluating tobool
. If a given expression evaluates totrue
a corresponding CSS class is added - otherwise it is removed.
While the NgClass directive can interpret expressions evaluating to
String
, Array
or Object
, the Object
-based version is the most often
used and has an advantage of keeping all the CSS class names in a template.
Examples
Try the live example.
See the Template Syntax section on ngClass
for more details.
- Implements
-
- DoCheck
- OnDestroy
- Annotations
- Directive(selector: "[ngClass]", inputs: const ["rawClass: ngClass", "initialClasses: class"])
Constructors
- NgClass(IterableDiffers _iterableDiffers, KeyValueDiffers _keyValueDiffers, ElementRef _ngEl, Renderer _renderer)
Properties
- hashCode → int
-
Get a hash code for this object.
read-only, inherited - initialClasses → String
-
write-only
- rawClass → dynamic
-
write-only
- runtimeType → Type
-
A representation of the runtime type of the object.
read-only, inherited
Operators
-
operator ==(
other) → bool -
The equality operator.
inherited
Methods
-
ngDoCheck(
) → void -
ngOnDestroy(
) → void -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a non-existent method or property is accessed.
inherited -
toString(
) → String -
Returns a string representation of this object.
inherited