ngClass in host property of component decorator does not work

ngClass is a directive and can't be used in host bindings. Host bindings only supports

  • property '[propName]':'expr'
  • attribute '[attr.attrName]':'expr'
  • event (event)="someFunction(event);otherExpr",
  • style [style.width]="booleanExpr"
  • class [class.className]="booleanExpr" binding.
  • class [class]="expr" where expr is a string with space separated classes

Here are two different ways to bind a host element class to a property using the @HostBinding decorator:

@HostBinding('class.enabled') private get isEnabled() { // use getter to reflect external value
    return this.selectionService.state.isEnabled;
}
@HostBinding('class.selected') private isSelected = false; // setting this add/removes 'selected' class

constructor(private selectionService: SelectionService) {
    this.selectionService.select$.subscribe(isSelected => {
        this.isSelected = isSelected;
    });
}