Video lecture
Watch on YouTube(opens in a new tab)Sub topic 3
Displaying Data with Interpolation and Template Expressions
This chapter assumes you already know how a component pairs a class with a template. It also assumes basic TypeScript and HTML. Interpolation is the simplest way to move data from the class into the page. It also introduces template expressions, which every later binding uses.
The working model
Interpolation uses double curly braces: {{ expression }}. Angular evaluates the expression against the component instance, turns the result into a string, and writes it into the DOM. When the data changes, Angular evaluates the expression again and updates the text.
A worked example
import { Component } from '@angular/core';
interface User {
first: string;
last: string;
manager?: { name: string };
}
@Component({
selector: 'app-profile',
template: `
<h2>{{ user.first }} {{ user.last }}</h2>
<p>Full name: {{ fullName() }}</p>
<p>Items in cart: {{ items.length }}</p>
<p>Total: {{ price * items.length }}</p>
<p>Status: {{ isAdmin ? 'Administrator' : 'Member' }}</p>
<p>Manager: {{ user.manager?.name ?? 'None' }}</p>
<img src="avatar.png" alt="Photo of {{ user.first }}" />
`
})
export class ProfileComponent {
user: User = { first: 'Ada', last: 'Lovelace' };
items = ['pen', 'notebook'];
price = 4;
isAdmin = false;
fullName() {
return `${this.user.first} ${this.user.last}`;
}
}The example shows these rules in practice:
- Names resolve against the component, so you write
user.first, notthis.user.first. - Expressions can use property access, method calls, arithmetic, the ternary operator, optional chaining (
?.), and nullish coalescing (??). - Interpolation also works inside attribute values.
alt="Photo of {{ user.first }}"sets thealtproperty. nullandundefinedrender as an empty string. They never render as the word "null".
What template expressions cannot do
Template expressions are a restricted subset of JavaScript. Several things are not allowed:
| Not allowed | Why |
|---|---|
| Assignment (`=`, `+=`), `++`, `--` | Expressions must not change state |
| `new` | Templates do not construct objects |
| Statements chained with `;` | Only a single expression is allowed |
| Globals such as `window`, `Math`, `console` | Only component members are in scope |
Reading a global such as Math fails because it is not a member of the component. To use it, expose it on the class first, for example with math = Math;.
Common errors
- Undefined object paths.
{{ user.manager.name }}throws an error ifmanageris missing. Use?.instead. - Displaying objects. An object renders as
[object Object]. Interpolate one of its properties, or format the value with a pipe, which a later chapter covers. - Strict template type errors. With
strictTemplatesenabled, a misspelled property or a wrong type is a compile error. This catches real bugs, so keep it on.
Exercise
A template contains {{ count++ }} and {{ Math.round(score) }}. Why does each fail, and how would you fix them?
Show answer
count++ is an increment, which template expressions forbid because they must not change state. Move the increment into a class method that an event calls. Math is a global, not a component member, so it is not in scope. Expose it with math = Math; and write {{ math.round(score) }}, or compute the rounded value in the class.
Exercise
What does <p>{{ user.nickname }}</p> show when nickname is undefined?
Show answer
An empty paragraph. Interpolation renders null and undefined as an empty string.