Sub topic 5
Binding to DOM Properties
This chapter assumes you know the difference between HTML attributes and DOM properties. It also assumes you have read the earlier chapters on interpolation and on reading signals in templates.
Attributes Versus Properties
When the browser parses HTML, it builds DOM objects. Attributes are the initial text values written in the markup. Properties are the live values on the resulting JavaScript objects. <input value="Ada"> sets the value attribute once. After the user types, the input's value property holds the current text and the attribute stays "Ada".
Angular's property binding writes to the property, not the attribute. Square brackets around a name mark the binding target:
<button [disabled]="isSaving()">Save</button>Angular evaluates the expression isSaving() and assigns the result to button.disabled. Every time change detection runs, Angular re-evaluates the expression and updates the property if the value changed.
A Working Example
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-profile',
template: `
<img [src]="avatarUrl()" [alt]="name() + ' avatar'" width="64" />
<input [value]="name()" [readOnly]="locked()" />
<button [disabled]="locked()" (click)="locked.set(true)">Lock</button>
`
})
export class ProfileComponent {
name = signal('Ada Lovelace');
avatarUrl = signal('https://example.com/ada.png');
locked = signal(false);
}Notes on the example:
[disabled]receives a real boolean. The attribute formdisabled="false"would still disable the button, because the attribute's presence is what counts.width="64"has no brackets, so it is a plain static attribute.- The binding goes one way. Typing in the input does not change
name. Writing back to the class needs event binding or two-way binding, which later chapters cover.
Interpolation inside an attribute, such as src="{{ avatarUrl() }}", compiles to a property binding too. It always produces a string, though. Use brackets when the property needs a boolean, a number, or an object.
Common Errors
| Mistake | What happens | Fix |
|---|---|---|
| `disabled="isSaving()"` without brackets | The attribute is set to the literal text `isSaving()`, so the button is always disabled | Write `[disabled]="isSaving()"` |
| `[data-id]="id()"` | Compile error: "Can't bind to 'data-id' since it isn't a known property" | Use an attribute binding, covered in a later chapter |
| `[src]="avatarUrl"` without calling the signal | The property receives the signal function itself | Call the signal: `avatarUrl()` |
| `[readonly]` vs `[readOnly]` | Angular maps some common names, but property names are case-sensitive | Use the DOM property name when in doubt |
Exercise
A template contains <input type="checkbox" checked="{{ agreed() }}" /> where agreed is signal(false). Why might this not behave as intended, and how do you fix it?
Show answer
Interpolation converts the value to the string "false". Assigning any non-empty string to the boolean checked property makes it true, so the box renders checked. Use [checked]="agreed()" so the property receives the actual boolean.
Exercise
Which binding updates when the user types into <input [value]="name()" />: the name signal, the input's value property, or both?
Show answer
Only the input's value property changes, because the user typed into it. Property binding runs one way, from class to DOM, so the name signal stays the same until code sets it.