Class/Style Binding
Step 1: Class Binding (The Boolean Toggle)
Class binding is used to add or remove CSS classes based on a condition. This is the preferred method because it keeps your CSS logic inside your stylesheet.
A. Toggling a Single Class
The syntax is [class.name-of-class]="condition"
B. Managing Multiple Classes (Object Syntax)
When a component has multiple states (e.g., Selected, Warning, Disabled), you can pass an object where the keys are class names and the values are booleans.
Step 2: Style Binding (Granular Control)
Style binding is used for values that are calculated at runtime and cannot be predefined in a CSS file, such as a progress bar width or a dynamic color picker.
A. Single Property with Units
Angular allows you to specify the unit (px, %, em) directly in the binding.
B. Multi-Style Objects
Just like classes, you can use an object to apply several inline styles simultaneously.
Step 3: Real-World Implementation (Microservice Monitor)
Let's combine everything into a "Service Health" card. We will use Event Binding to update the state, which in turn updates the Class and Style bindings.
component.ts
component.html
| Binding Type | Syntax | Best Use Case |
| Single Class | [class.name]="bool" | Simple states (Active, Hidden, Selected). |
| Multi Class | [class]="object" | Complex UI states with overlapping rules. |
| Single Style | [style.prop]="val" | Calculated values (Width, Height, Color). |
| Multi Style | [style]="object" | Grouped visual transformations. |