Sub topic 10
Attribute, Class, and Style Bindings
This chapter assumes you know property binding ([prop]="expr"), event binding, and how to read signals in templates. Property binding sets DOM properties. Some things you want to bind are not DOM properties, though: ARIA attributes, colspan, and individual CSS classes or style declarations. Angular gives each of these its own binding prefix.
Attribute bindings with `attr.`
An attribute binding sets an HTML attribute instead of a DOM property. Use it when no matching property exists. ARIA attributes, colspan on a <td>, and SVG attributes are the usual cases.
If the expression evaluates to null or undefined, Angular removes the attribute entirely. That is how you make an attribute conditional.
Class bindings
A single class binding, [class.name]="condition", adds the class when the expression is truthy and removes it when it is falsy.
A multi-class binding, [class]="expr", accepts three forms:
| Expression type | Example | Result |
|---|---|---|
| String | `'large rounded'` | Adds both classes |
| Object | `{ large: true, rounded: false }` | Adds keys with truthy values |
| Array | `['large', 'rounded']` | Adds every entry |
A static class="bar" on the same element is merged with the bound classes, so you don't lose it.
Style bindings
A single style binding, [style.property]="value", sets one CSS property. You can add a unit suffix: [style.width.px]="200" or [style.width.%]="50". Property names work in dash-case (background-color) or camelCase (backgroundColor). [style]="expr" accepts a string such as 'width: 100px' or an object of property/value pairs.
A complete example
import { Component, computed, signal } from '@angular/core';
@Component({
selector: 'app-progress',
template: `
<div class="track">
<div
class="bar"
role="progressbar"
[attr.aria-valuenow]="percent()"
[attr.aria-label]="label() || null"
[class.complete]="percent() === 100"
[class]="sizeClasses()"
[style.width.%]="percent()"
[style.background-color]="color()">
</div>
</div>
<button (click)="advance()">Advance</button>
`,
styles: `
.track { background: #eee; }
.bar { height: 1rem; transition: width 0.3s; }
.large { height: 2rem; }
.complete { outline: 2px solid green; }
`
})
export class ProgressComponent {
percent = signal(20);
label = signal('');
large = signal(true);
sizeClasses = computed(() => ({ large: this.large() }));
color = computed(() => (this.percent() < 50 ? 'orange' : 'seagreen'));
advance() {
this.percent.update(p => Math.min(p + 20, 100));
}
}The aria-label attribute is missing from the DOM until label() is non-empty, because '' || null evaluates to null.
Common errors
- Writing
[aria-label]="x"fails with "Can't bind to 'aria-label' since it isn't a known property". Use[attr.aria-label]. - Forgetting the unit:
[style.width]="50"sets no valid width. Use[style.width.px]="50". NgClassandNgStylestill work, but native[class]and[style]bindings cover the same cases without an import.
Exercise
You need a table cell that spans cols() columns and gets the class highlight only when selected() is true. Write the opening <td> tag.
Show answer
<td [attr.colspan]="cols()" [class.highlight]="selected()">. You need attr. because the binding targets the colspan attribute; the matching DOM property is colSpan, with a capital S.
Exercise
A component holds styles = { color: 'red' } and a button handler runs this.styles.color = 'blue'. The template uses [style]="styles", but the text stays red. Why, and how do you fix it?
Show answer
Angular checks object bindings by reference, so an in-place mutation goes unnoticed. Assign a new object, this.styles = { ...this.styles, color: 'blue' }, or keep the value in a signal and call set with a new object.