Interpolation and property, attribute, class, and style binding
The four ways an Angular template connects to component data, and when to reach for each one.
Interpolation and property, attribute, class, and style binding
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Interpolation (`{{ }}`) writes a string into text content or an attribute position — it's always rendering text, never setting a DOM property directly.
- Property binding (`[property]`) sets a DOM property, not an HTML attribute — that distinction is why `[disabled]="isLoading"` works with a boolean and `disabled="isLoading"` wouldn't.
- `[attr.]`, `[class.]`, and `[style.]` are property binding's specialized siblings, for the handful of cases — ARIA attributes, single classes, single style properties — where you need the attribute itself, not a property.
The question every binding syntax is answering
A template needs to show live data, not a snapshot. <p>Hello, {{ name }}</p>
should update the moment name changes, without you writing a line of DOM-update
code yourself. That’s the entire job of Angular’s binding syntax — different
flavors of “keep this part of the DOM in sync with that part of my component,”
each flavor suited to a different kind of target.
There are four of them, and mixing them up is the single most common template mistake — not because the syntax is hard, but because HTML attributes and DOM properties look identical in a template and are not, in fact, the same thing.
Interpolation: text, always
<h1>Welcome, {{ userName }}</h1>
<img src="/avatars/{{ userId }}.png" />
{{ expression }} evaluates the expression and drops the result in as text —
either as element content, or spliced into an attribute’s string value, as in the
src example above. It’s the simplest binding and the one you’ll use constantly,
but it’s worth being precise about what it is: it always produces a string, in a
text position. It never sets a DOM property on its own.
Property binding: the DOM, not the HTML
<button [disabled]="isSaving">Save</button>
<img [src]="avatarUrl" />
This is where the attribute-vs-property distinction actually bites. An HTML
attribute is what you write in markup — static, always a string. A DOM
property is a live field on the actual element object in memory, and it can be
any type: a boolean, a number, an object. [disabled]="isSaving" sets the
disabled property directly, so when isSaving is false, the property is
false and the button is enabled — correctly. If you instead wrote
disabled="{{ isSaving }}", you’d be interpolating the string "false" into the
disabled attribute, and in HTML, an attribute’s bare presence (regardless of
its string value) is often enough to trigger the behavior — so the button could
end up disabled when you meant it not to be.
Property binding is the default tool for “set this thing on the element based on
my data.” Most of what you bind — src, value, disabled, textContent — goes
through [property], not interpolation, once it’s more than plain text content.
[attr.], [class.], [style.]: when there’s no property to bind
Some things you want to control simply don’t exist as DOM properties.
aria-expanded, colspan, and most ARIA and SVG attributes are attribute-only —
there’s no corresponding JavaScript property on the element to set. For those,
[attr.] binds to the attribute itself:
<button [attr.aria-expanded]="isOpen">Toggle</button>
[class.] and [style.] are the same idea applied to a narrower, extremely
common case — toggling one class or one inline style property based on a
boolean or value:
<div [class.active]="isSelected" [style.color]="textColor"></div>
You could express both of those through a general [ngClass]/[ngStyle]
directive instead, but for a single class or a single style property, the
dot-syntax binding is the more direct tool — no extra directive, no object
literal, just the one thing you’re toggling.
Precedence, in one sentence
If a DOM property exists for what you’re setting, bind the property. Reach for
[attr.] only when no property exists for what you need, and reach for
[class.]/[style.] specifically for the single-class, single-style-property
case. This lesson’s video covers interpolation in depth; the two linked
“deeper” videos pick up exactly where it leaves off, walking through class/style
and attribute binding with their own worked examples.
Check yourself
Why does `<button [disabled]="isSaving">Save</button>` work correctly with a boolean, while `<button disabled="{{ isSaving }}">Save</button>` behaves oddly?
`[disabled]` is a property binding — it sets the DOM `disabled` property directly to whatever `isSaving` evaluates to (true or false). `disabled="{{ isSaving }}"` interpolates a string into the HTML attribute, and the `disabled` attribute's mere presence (even as the string `"false"`) makes a button disabled — so interpolating into it gets the boolean logic wrong.
When would you reach for `[attr.aria-expanded]` instead of a plain property binding?
When there's no matching DOM property to bind to — `aria-expanded` and most ARIA attributes exist only as HTML attributes, not as properties on the DOM element object. `[attr.]` binds directly to the attribute itself, which is exactly what a case like this needs.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.