TExcelTAD
Home › Angular › Property Binding

Property Binding

Interpolation inserts text. Property binding goes further: it sets a property of a DOM element, component or directive to the value of an expression. The syntax is square brackets around the target name: [property]="expression".

Binding to element properties

angular · try it yourself
import { Component, signal } from '@angular/core';

@Component({
  selector: 'app-profile',
  template: `
    <img [src]="avatarUrl()" [alt]="name()" width="64" height="64" />
    <input [value]="name()" [disabled]="locked()" />
    <button [disabled]="locked()">Save</button>
  `,
})
export class Profile {
  name = signal('Kenji Sato');
  avatarUrl = signal('/avatars/kenji.png');
  locked = signal(true);
}

Without brackets, disabled="locked()" would set the attribute to the literal string "locked()". With brackets, Angular evaluates the expression and keeps the property in sync whenever the signal changes.

Note: [value]="name()" and value="{{ name() }}" produce the same result for strings. Prefer brackets — they work for any type (booleans, numbers, objects), while interpolation always produces a string.

Properties vs attributes

HTML attributes initialise the page; DOM properties hold the live state. Angular binds to properties. A few attributes have no matching property — for those, use the attr. prefix:

angular · try it yourself
import { Component } from '@angular/core';

@Component({
  selector: 'app-table',
  template: `
    <table border="1" cellpadding="6">
      <tr><td [attr.colspan]="columns">Quarterly report</td></tr>
      <tr><td>Q1</td><td>Q2</td><td>Q3</td></tr>
    </table>
    <button [attr.aria-label]="closeLabel">✕</button>
  `,
})
export class ReportTable {
  columns = 3;
  closeLabel = 'Close the report';
}

Class and style bindings

Toggling a single CSS class or style is so common that it has its own shortcuts:

Binding Effect
[class.done]="isDone()" Adds class done when the expression is truthy
[class]="{ done: isDone(), urgent: isUrgent() }" Sets several classes from an object
[style.color]="color()" Sets one style property
[style.width.px]="width()" Sets a style with a unit suffix
[style]="{ 'font-size': size() + 'px' }" Sets several styles from an object
angular · try it yourself
import { Component, signal } from '@angular/core';

@Component({
  selector: 'app-progress',
  template: `
    <div class="track">
      <div class="bar" [style.width.%]="percent()" [class.complete]="percent() === 100"></div>
    </div>
    <p [style.color]="percent() < 50 ? 'gray' : 'green'">{{ percent() }}% uploaded</p>
  `,
  styles: `
    .track { background: #eee; border-radius: 6px; height: 14px; }
    .bar { background: #dd0031; height: 100%; border-radius: 6px; }
    .bar.complete { background: #2e7d32; }
  `,
})
export class Progress {
  percent = signal(70);
}

Binding is one-way

Property binding flows from the component to the view. If the user types into an <input [value]="name()">, the signal does not change. You'll combine it with event binding (next lesson) or use two-way binding to go the other direction.

The idea of "evaluate the expression, then copy the result onto the target only if it changed" can be sketched in plain TypeScript:

ts · try it yourself
interface FakeElement {
  disabled: boolean;
  title: string;
}

const el: FakeElement = { disabled: false, title: '' };
let writes = 0;

function bindProperty<K extends keyof FakeElement>(key: K, value: FakeElement[K]): void {
  if (el[key] !== value) {
    el[key] = value;
    writes++;
  }
}

bindProperty('disabled', true);
bindProperty('title', 'Saving…');
bindProperty('disabled', true); // unchanged: no DOM write
console.log(el);
console.log('DOM writes:', writes);
{ disabled: true, title: 'Saving…' }
DOM writes: 2

Tip: Angular compares the new value with the previous one and only touches the DOM when it differs — which is why bindings are cheap even on large pages.

Test yourself

What does <button disabled="isBusy"> do without brackets?

  1. Evaluates the isBusy property
  2. Sets the attribute to the literal string "isBusy", which disables the button
  3. Throws a compile error
  4. Nothing

Which binding adds the class active only when selected() is true?

  1. [active]="selected()"
  2. class="{{active}}"
  3. [class.active]="selected()"
  4. (class.active)="selected()"

When should you use [attr.colspan] instead of [colspan]?

  1. Always
  2. When binding to an HTML attribute that has no matching DOM property
  3. Only for numbers
  4. Only inside @for