Skip to the workspace
Pebellslearn by building
Understand Binding  ›  Lesson

Property and Attribute Binding

Property Binding 12 minSquare brackets bind an expression to a DOM property of an element or an input of a component, and the attr. prefix binds to plain HTML attributes that have no matching property.

angular / Property and Attribute Binding

Explore each scene at your own pace.

01 · The rule

The rule

Property and Attribute Binding

The plant card needs a photo whose address comes from data, a button that turns grey while watering is in progress, and a red border when the plant is thirsty. Each of these sets something on an element. That is the job of square brackets.

The rule

Brackets mean "evaluate this"; no brackets means "this exact text".

Property binding puts the result of an expression into a DOM property, or into a component input:

<img [src]="plant().photo" [alt]="plant().name" />
<button [disabled]="watering()">Water</button>
<app-plant-card [plant]="p" />

Without brackets, src="plant().photo" is plain HTML: the browser gets the literal characters plant().photo as the address.

Two handy shapes for styling:

<article [class.thirsty]="isThirsty()">...</article>
<div class="gauge" [style.width.%]="waterLevel()"></div>

[class.thirsty] adds the class while the expression is true and removes it when false. [style.width.%] sets one style, with the unit written after the dot.

Attribute binding exists because not every HTML attribute has a DOM property. ARIA attributes, data-* attributes and many SVG attributes are like this. Prefix the name with attr.:

<button [attr.aria-label]="'Water ' + plant().name">💧</button>
<li [attr.data-plant-id]="plant().id">...</li>

If the bound value is null, Angular removes the attribute completely, which is how you make one optional.

Why the difference? An HTML attribute is the starting text in the markup; a DOM property is the live value on the element object. disabled has a property, so [disabled] works. data-plant-id has no property at all, so [data-plant-id] fails with "not a known property", and [attr.data-plant-id] fixes it. (Recent Angular versions also accept ARIA names without the prefix, but [attr.aria-label] works in every version and says plainly that you are setting an attribute.)

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.