[ Web Proxy ]
URL:
Viewing: https://angular.dev/api/common/NgClass [Back]  [Original]

NgClass Angular Skip to main content
menu
close
more_horiz
menuAPI
@angular/common

NgClass

directive
stable

Adds and removes CSS classes on an HTML element.

Class bindings

On this page

    arrow_upward_alt Back to the top

    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

    NgClass
    @param_ngElElementRef<any>
    @param_rendererRenderer2
    @returnsNgClass

    klass

    string

    ngClass

    string | string[] | Set<string> | { [klass: string]: any; } | null | undefined

    ngDoCheck

    void
    @returnsvoid

    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>
    Jump to details

    Web Proxy Viewer  |  New URL  |  Original Page