Angular Template Syntax

Template syntax is the way a template (the component's HTML) reads data from its component class. Interpolation, {{ expression }}, shows the result of an…

In the previous lesson, the menu card's template had {{ name }} and {{ '$' + price }}, and the name and price appeared on the page. Those double curly braces are one form of template syntax: the way you write in a template (the component's HTML) to read data from its component class. There are three basic forms: {{ }} for text, [ ] for an element's property (a value the element holds in the DOM, such as src on an <img>), and [attr. ] for an HTML attribute.

Picture a menu board at Jali's Pancake Stall where the price numbers are magnets. The menu names are painted on permanently. The price numbers are stuck on and can be changed at any time. In Angular, the template is the board, {{ price }} is the place for a magnet, and the component class is Jali, who sticks on the new number. As soon as the value of price changes in the class, the number on the board changes with it.

Interpolation: {{ }}

Interpolation means putting a value from the class into text with {{ }}. Here the class App has three properties. Its template shows all three:

import { Component } from '@angular/core'

@Component({
  selector: 'app-root',
  standalone: true,
  template: `
    <h1>{{ title }}</h1>
    <p>{{ menu }}: {{ '$' + price }}</p>
    <p>Two portions: {{ '$' + price * 2 }}</p>
    <p>price * 2</p>
  `,
})
export class App {
  title = "Jali's Pancake Stall"
  menu = 'Special Egg Pancake'
  price = 10
}

What the page shows:

Jali's Pancake Stall
Special Egg Pancake: $10
Two portions: $20
price * 2

Template expressions have limits. The Angular compiler (the program that turns templates into JavaScript) refuses an expression that puts a value into something (an assignment) or uses new. One error in a template is enough to stop the whole component from showing:

Written Message from the compiler (Angular 18)
{{ price = 0 }} Parser Error: Bindings cannot contain assignments
{{ new Date() }} Parser Error: Unexpected token 'Date'

Property binding: [ ]

Square brackets are used for property binding. Binding means linking: the value on the element comes from the class, and changes when the value in the class changes. The template and its class properties:

<img [src]="photo" [alt]="menu" />
<button [disabled]="soldOut">Order</button>
photo = 'egg-pancake.jpg'
soldOut = false

Attribute, class and style binding

Three more bindings also use square brackets:

<td [attr.colspan]="columnSpan">Value Pack</td>
<p [class.sold-out]="soldOut">Pandan Pancake</p>
<div [style.width.px]="barWidth"></div>

Which one to use

You want... Write
text inside a tag {{ menu }}
an element property (src, disabled, value) [src]="photo"
an attribute with no property of the same name (colspan, aria-label) [attr.colspan]="columnSpan"
one class on or off [class.sold-out]="soldOut"
one style with a unit [style.width.px]="barWidth"

⚠️ Wrong brackets, wrong values

TL;DR: Template syntax is the way a template (the component's HTML) reads data from its component class. Interpolation, {{ expression }}, shows the result of an expression (code that produces a value) as text: {{ price * 2 }} with price = 10 becomes 20. If the result holds HTML, its tags show as characters. Angular refuses assignments (putting a value into something) or new inside {{ }}. Property binding, [property]="expression", sets the result of an expression on a DOM property (a value the element holds), such as [src]="photo" and [disabled]="soldOut". Without brackets the value is just text as written. [attr.colspan] is for an attribute with no property of the same name, [class.sold-out] for one class, and [style.width.px] for one style with a unit. Never put {{ }} inside [ ].

⚡ {{ }} shows an expression's result

Write:

<p>{{ menu }}: {{ '$' + price }}</p>
<p>Two portions: {{ '$' + price * 2 }}</p>
<p>price * 2</p>
Guess first: with price = 10, what does the third line show?

The words price * 2, as written. Without curly braces, that line is just plain text. The line that calculates is the second one: Two portions: $20.

Meaning: This template reads two class properties (variables that belong to the class): menu and price. Inside {{ }} sits an expression, which is code that produces one value, and its result always becomes text. The price shows with no cents.

⚡ [ ] sets an element's property

Write:

<img [src]="photo" [alt]="menu" />
<button [disabled]="soldOut">Order</button>
Guess first: the result of {{ }} is always text. With soldOut = false, does disabled="{{ soldOut }}" disable the button?

Yes. Any text that isn't empty, "false" included, counts as true. Write [disabled]="soldOut".

Meaning: This is property binding: square brackets read an expression from the class, then set its result on the element's DOM property (a value the element holds, such as src). Here photo and soldOut are class properties. Without brackets, src="photo" sends the text photo as it is.

The second exercise refuses any src= typed directly on <img>, so write [src].

⚡ Attributes, classes and styles

Write:

<td [attr.colspan]="columnSpan">Value Pack</td>
<p [class.sold-out]="soldOut">Pandan Pancake</p>

Meaning: Property binding only works with a property the DOM element has. The table cell td has the property colSpan, not colspan, so [colspan] is refused with error NG0303. [attr.colspan] writes the attribute itself into the HTML. [class.sold-out] adds the CSS class sold-out while the property soldOut is true.

What you will learn