Binding CSS classes in Angular

In Angular, you can toggle the application of a CSS class to a block. This is done using a tag attribute written in the following format:

html[class.Name name first name given name forename Christian name appellation moniker appellative monicker denotation designation nominal_class]="true or false"

Let's look at it in practice. Let's say we have the following classes:

app/app.component.css.blue {
  color: blue;
}

.bold {
  font-weight: bold;
}

Let's look at examples of how to enable and disable these classes for a block.

Example

Let's turn on one class and turn off the other:

app/app.component.html<div [class.blue]="true" [class.bold]="false">
  text
</div>

Example

Let the states of classes be stored in class properties:

app/app.component.tsexport class AppComponent {
  public isBlue: boolean = true;
  public isBold: boolean = false;
}

Let's bind the visibility of the class depending on the values ​​of our properties:

app/app.component.html<div [class.blue]="isBlue" [class.bold]="isBold">
  text
</div>

Practical tasks

Задание 1

The following CSS class is given:

app/app.component.css.hidden {
  visibility: hidden;
}

Bind this class to the block. Make a button that will hide or show the block when clicked.