← Back to Angular, page 6

angular-0102 · Easy fresher

What is the difference between an HTML attribute and a DOM property, and which binding gotchas does this cause in Angular templates?

An attribute is what you write in HTML and sets an initial value, while a property is the live value on the DOM object. Angular property binding sets properties, so [value] changes what the user sees, but some things only exist as attributes (aria-*, colspan), and these need [attr.name].

Full answer

When the browser reads <input value="Asha"> it creates a DOM object. The attribute value="Asha" is only the initial value. The property input.value is the current value, and it changes when the user types. The attribute does not change.

Angular bindings work with properties, not attributes:

  • [value]="name" sets input.value. Interpolation value="{{ name }}" does the same thing, because Angular turns it into a property binding.
  • [disabled]="isLocked" sets button.disabled to true or false. The attribute disabled="false" in plain HTML still disables the button, because only the presence of the attribute matters. That is why you must bind with brackets and never write disabled="{{ flag }}" expecting a boolean.

Things with no matching property need an attribute binding with the attr. prefix:

  • [attr.aria-label]="label" and other ARIA attributes.
  • [attr.colspan]="span" (the DOM property is colSpan, with a capital S, and not every table attribute has a property).
  • [attr.data-id]="id". Setting the value to null removes the attribute.

Other gotchas:

  • Interpolation always converts to a string, so title="{{ count }}" gives "5". A property binding passes the real type: [maxLength]="5" passes a number.
  • Binding to an unknown property is a compile error: Can't bind to 'foo' since it isn't a known property. This often means a missing import in a standalone component.
  • A plain attribute with no brackets passes a fixed string: <app-card size="3"> gives the string "3", and [size]="3" gives the number 3.
  • Class and style have special bindings: [class.active]="isActive" and [style.width.px]="width".
typescript
import { Component, signal } from '@angular/core';

@Component({
  selector: 'app-binding-gotchas',
  template: `
    <input [value]="name()" (input)="name.set($any($event.target).value)" />
    <button [disabled]="locked()" [attr.aria-label]="label()">Save</button>
    <button (click)="locked.set(!locked())">Toggle lock</button>
    <button (click)="label.set(null)">Clear label</button>
    <table><tr><td [attr.colspan]="span">Wide cell</td></tr></table>
  `,
})
export class BindingGotchasComponent {
  readonly name = signal('Asha');
  readonly locked = signal(false);
  readonly label = signal<string | null>('Save marks');
  readonly span = 2;
}

Angular 20.3, strict template check passes. A Karma test checks the ARIA attribute value, that the attribute is removed when the value is null, and the colspan attribute.

Key points

  • Attributes set initial values. Properties are the live DOM state. Angular binds properties.
  • Use `[attr.name]` for attributes without a property, such as `aria-*`.
  • Interpolation produces strings. Property binding keeps the type.
  • Without brackets, an input receives a literal string.

Common mistakes

  • Writing `disabled="{{ flag }}"` or `disabled="false"` and wondering why the control stays disabled.
  • Forgetting `attr.` for ARIA attributes.
  • Passing `size="3"` to a numeric input and getting the string "3".

Follow-ups

  • What happens when you bind `[attr.title]` to `null`?
  • Why does `[value]` update the input but the `value` attribute stay the same?
  • How would you pass a number to a component input from a template?

Tips

  • Use the `<input value>` example. It makes the difference clear in one line.
  • Mention `attr.` for accessibility attributes.
  • Mention the string versus typed value difference.

Evaluation rubric

  • Defines attribute and property correctly.
  • Names `[attr.x]` use cases and the boolean attribute trap.
  • Knows interpolation gives strings.