Property Binding

Square brackets bind an element's property to the component's data, such as [src]="imageUrl" or [disabled]="soldOut". [class.name] turns a CSS class on and off, and [style.prop] sets one style, all kept up to date by Angular.

Apprentice · Angular · Updated

After this lesson you can

  • Bind an element's properties, classes and styles to component data with square brackets

Key takeaways

  • [src]="imageUrl" sets an element's property to the value of imageUrl; without brackets, src="imageUrl" is just the text imageUrl.
  • [class.text-muted] adds a class while its expression is true, and [style.width.%]="rating" sets one style, with its unit.
  • Use braces for text between tags and brackets for properties: [disabled] gets a real true or false, where braces would give it text.
  • A binding isn't a one-time copy: when the component's data changes, Angular updates the element.

Interpolation puts data between tags. To put it in an element's attributes, such as an image's src or a button's disabled, use property binding: the property's name in square brackets, and an expression in quotes.

A product card

import { Component } from '@angular/core';

@Component({
  imports: [],
  selector: 'app-product-card',
  templateUrl: './product-card.html',
})
export class ProductCard {
  protected readonly name = 'Chai';
  protected readonly imageUrl = 'images/chai.jpg';
  protected readonly unitsInStock = 0;
  protected readonly rating = 80;
}
<div class="card">
  <img [src]="imageUrl" [alt]="name" class="card-img-top">
  <div class="card-body">
    <h2 class="h5" [class.text-muted]="unitsInStock === 0">{{ name }}</h2>
    <div class="progress mb-2">
      <div class="progress-bar" [style.width.%]="rating"></div>
    </div>
    <button class="btn btn-primary" [disabled]="unitsInStock === 0">Add to cart</button>
  </div>
</div>

The page gets:

<img class="card-img-top" src="images/chai.jpg" alt="Chai">
<h2 class="h5 text-muted">Chai</h2>
<div class="progress-bar" style="width: 80%;"></div>
<button class="btn btn-primary" disabled="">Add to cart</button>

The kinds of binding

Binding Does Here
[src]="imageUrl" Sets a property to the expression's value The image's address
[disabled]="unitsInStock === 0" A true/false property: on when the expression is true Can't add an item that's out of stock
[class.text-muted]="unitsInStock === 0" Adds the class when true, removes it when false Grays out the name
[style.width.%]="rating" Sets one style, with a unit after the second dot The bar is 80% wide

The quotes hold an expression, not text. [src]="imageUrl" means the value of imageUrl. Without brackets, src="imageUrl" would be the text imageUrl, as in plain HTML.

Brackets or braces?

<img src="{{ imageUrl }}"> would work too, for text. But brackets are the clear choice for anything that isn't text:

  • [disabled]="unitsInStock === 0" sets a real true or false. With braces, disabled="{{ ... }}" would set the text "false", and, as in plain HTML, a disabled attribute with any value at all disables the button.
  • Brackets say plainly: this property comes from the component.

So: braces for text between tags, brackets for properties.

It stays up to date

A binding isn't a one-time copy. When the component's data changes, Angular updates the element: when unitsInStock goes up from 0, the button enables itself and the name stops being gray. You'll see that in action as soon as the component's data can change, in the next lessons.

Check yourself

1 question to try on the lesson page.

So what

Square brackets connect an element's properties to your component's data: [src], [disabled], [class.x], [style.y]. Write the rule once in the template, and Angular keeps the page matching the data.

Key terms

Cheat sheets