Attribute Directives

  • Change the appearance or behavior of an element, component, or another directive.

Built-in Attribute Directives

Directives
Details

ngClass

Adds or removes a set of CSS classes.

ngStyle

Adds and removes a set of HTML styles.

ngClass

  • with expression

<!-- toggle the "special" class on/off with a property -->
<div [ngClass]="isSpecial ? 'special' : ''">This div is special</div>
  • with method

app.component.ts
currentClasses: Record<string, boolean> = {};
/* . . . */
setCurrentClasses() {
  // CSS classes: added/removed per current state of component properties
  this.currentClasses =  {
    saveable: this.canSave,
    modified: !this.isUnchanged,
    special:  this.isSpecial
  };
}

ngStyle

Last updated

Was this helpful?