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
- Inside
{{ }}sits an expression: a piece of code that produces one value. A name inside it, such asprice, is read from the class.{{ price * 2 }}withprice = 10produces20, and'$' + price * 2puts a dollar sign in front of it. Without curly braces,price * 2is just plain text and shows as written. - The result of an expression is always turned into text.
nullandundefinedshow as nothing. - The price shows as
$10, with no cents. Dollar formatting comes in the Pipes lesson. - Interpolation shows text, not HTML. If the property
description = '<b>Spicy</b>'is shown with{{ }}, you see the characters<b>Spicy</b>: the<b>tag doesn't make the text bold. That's on purpose, so text from users can't put HTML into your page.
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
- Square brackets mean: what's inside the quotes is an expression read from the class. Its result is set on that element's DOM property. So
[src]="photo"is the same as runningimg.src = this.photo(this.photois the class'sphotoproperty). - Without brackets,
src="photo"sends the textphotoas it is. The browser looks for a file calledphoto, and the image breaks. [disabled]="soldOut": ifsoldOutisfalse, the button is enabled. If it'strue, the button is disabled.src="{{ photo }}"also works when the value is text, because Angular turns it into a property binding. But for values that aren't text, such as booleans (trueorfalse), use[ ]. The second exercise refuses anysrc=typed directly on<img>, so write[src].- Don't mix the two:
[src]="{{ photo }}"is refused withParser Error: Got interpolation ({{}}) where expression was expected.
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>
- Property binding only works with a property the DOM element really has. The table cell
tdhas the propertycolSpan(capital S), notcolspan. So[colspan]="columnSpan"is refused:NG0303: Can't bind to 'colspan' since it isn't a known property of 'td'.[attr.colspan]writes the attribute itself into the HTML: withcolumnSpan = 2, you get<td colspan="2">. Use[attr. ]foraria-labeland similar attributes too. [class.sold-out]="soldOut"adds the CSS classsold-outto the element when the propertysoldOutistrue, and removes it when it'sfalse.[style.width.px]="barWidth"setswidthwith the unit px:barWidth = 120becomeswidth: 120px.
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
- Forgetting the brackets. In the second exercise,
src="imageUrl"sends the textimageUrl, not the image address stored in the propertyimageUrl. disabled="{{ soldOut }}". An interpolation result is always text, and any text that isn't empty,"false"included, counts as true. The button is disabled even thoughsoldOutisfalse. Write[disabled]="soldOut".{{ }}inside[ ]. The compiler refuses[src]="{{ photo }}". Inside brackets, write just the expression:[src]="photo".[colspan]. The result is error NG0303, because the property is calledcolSpan. Use[attr.colspan].
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.