Sub topic 3
Binding to DOM Properties
This chapter assumes you know HTML and DOM basics, can write a TypeScript class, and have already used interpolation in an Angular template. Interpolation puts text into the page. Property binding goes further: it sets any property of a DOM element, such as an image's src or a button's disabled state, from a component field.
The Syntax
A property binding wraps the target property name in square brackets. The right-hand side is a template expression that Angular evaluates against the component instance.
import { Component } from '@angular/core';
@Component({
selector: 'app-profile',
template: `
<h2>{{ name }}</h2>
<img [src]="avatarUrl" [alt]="name">
<button [disabled]="!canEdit">Edit</button>
`,
})
export class ProfileComponent {
name = 'Ada';
avatarUrl = '/assets/ada.png';
canEdit = false;
}Angular reads avatarUrl and assigns it to the src property of the <img> element. It evaluates !canEdit, gets true, and assigns that boolean to the button's disabled property. When canEdit changes later, Angular re-evaluates the expression and updates the property.
Data flows one way here: from the component to the DOM. If the user does something to the element, the field does not change. Sending changes back is the job of event binding, which a later chapter covers.
Properties Are Not Attributes
The target of [disabled] is the DOM property, a field on the live JavaScript object that the browser creates for the element. It is not the HTML attribute written in the markup. Attributes set an element's starting values. Properties hold its current state.
The difference matters most for boolean attributes. In plain HTML, <button disabled="false"> is still disabled, because the attribute's presence is what counts. A property binding avoids this, because it assigns a real boolean:
| Markup | What happens | Button disabled? |
|---|---|---|
| `disabled="false"` | Attribute present with the string "false" | Yes |
| `[disabled]="false"` | Property set to boolean `false` | No |
| `[disabled]="!canEdit"` | Property follows the field | Depends on `canEdit` |
Some attributes have no matching property, such as colspan on a <td> or ARIA attributes. Binding [colspan] there fails. The next chapter covers the attr. prefix, along with class and style bindings, for those cases.
Brackets Versus Interpolation
For string properties, these two lines produce the same result:
<img [src]="avatarUrl">
<img src="{{ avatarUrl }}">Angular converts the interpolated form into a property binding. Interpolation always yields a string, though, so prefer brackets for non-string values like booleans, numbers and objects.
Common Errors
- Forgetting the brackets.
<img src="avatarUrl">sets the literal textavatarUrlas the source. Without brackets, Angular does not evaluate anything. - Binding to a property that doesn't exist.
[colspan]or a misspelled name raisesCan't bind to 'colspan' since it isn't a known property of 'td'. Check the DOM property name, which can differ from the attribute name (for example,textContentorreadOnly). - Side effects in expressions. Angular may evaluate the expression many times. Keep it cheap and pure: read fields and avoid calls that change state or do heavy work.
- Quoting a string literal. To pass a constant string, nest quotes:
[alt]="'Profile photo'". Writingalt="Profile photo"without brackets also works.
Exercises
Exercise
A template contains <input disabled="{{ locked }}"> where locked is false. Why might this be a problem, and how do you fix it?
Show answer
Interpolation produces the string "false", and a non-empty string assigned to the disabled property is truthy, so the input ends up disabled. Use [disabled]="locked" so Angular assigns the boolean value.
Exercise
You write <td [colspan]="span"> and Angular reports "Can't bind to 'colspan'". What is the cause?
Show answer
The property binding targets a DOM property, and HTMLTableCellElement exposes colSpan, not colspan. Bind [colSpan]="span" or use an attribute binding, [attr.colspan]="span".