Sub topic 2
Displaying Values with Interpolation
This chapter assumes you can create and run a standalone Angular component with the Angular CLI. It also assumes you know TypeScript classes and the template expression rules from the previous chapter. Interpolation is the simplest binding form. It reads a value from the component and writes it into the page as text.
The working model
Interpolation uses double curly braces: {{ expression }}. Angular evaluates the expression against the component instance, converts the result to a string, and puts that string into the DOM. Data moves one way only, from the component to the view. When the component's state changes and change detection runs, Angular evaluates the expression again and updates the text if the result is different.
A worked example
import { Component } from '@angular/core';
import { DatePipe } from '@angular/common';
interface Customer {
name: string;
}
@Component({
selector: 'app-order-summary',
imports: [DatePipe],
template: `
<h2>Order #{{ orderId }}</h2>
<p>Customer: {{ customer?.name }}</p>
<p>Items: {{ items.length }}, total {{ total() }} EUR</p>
<p>Placed {{ placedAt | date: 'mediumDate' }}</p>
<p title="Order {{ orderId }} for {{ customer?.name }}">Hover for details</p>
<p>Note: {{ note }}</p>
`,
})
export class OrderSummaryComponent {
orderId = 1042;
customer: Customer | null = { name: 'Ada' };
items = [{ price: 12.5 }, { price: 7 }];
placedAt = new Date(2026, 10, 25);
note: string | null = null;
total(): number {
return this.items.reduce((sum, item) => sum + item.price, 0);
}
}This example shows several behaviors:
{{ orderId }}turns the number1042into the text"1042".{{ customer?.name }}uses optional chaining. Ifcustomerisnull, the expression returnsundefinedand does not throw an error.{{ note }}renders as an empty string becausenullandundefinedproduce no text. You will not see the words "null" or "undefined" on the page.{{ placedAt | date: 'mediumDate' }}passes the value through a pipe before display. A pipe is a formatting function you can use in a template. You must import the pipe, hereDatePipe, in the component'simports.title="Order {{ orderId }} ..."mixes literal text with interpolation inside an attribute value. Angular compiles this into a property binding that sets the element'stitleproperty to the combined string.
What happens to the value
| Expression result | Rendered text |
|---|---|
| `1042` | `1042` |
| `null` or `undefined` | empty string |
| `true` | `true` |
| `{ name: 'Ada' }` | `[object Object]` |
| `'<b>Hi</b>'` | `<b>Hi</b>` shown as literal text |
The last row matters for security. Interpolation writes text, so HTML inside a string is escaped and is never parsed as markup. A later chapter covers sanitization in detail.
Common errors
- Expensive method calls.
{{ total() }}runs on every change detection pass. A cheap reduce over two items is fine. Sorting thousands of rows inside a template is not. Store the computed value in a field, or use a pipe or a signal. - Side effects. Expressions should only read state. Assignments such as
{{ count = 5 }}are not allowed, and calling a method that changes state causesExpressionChangedAfterItHasBeenCheckederrors in development mode. - Displaying objects.
{{ customer }}prints[object Object]. Interpolate a specific property, or use thejsonpipe while debugging. - Missing null guards.
{{ customer.name }}fails strict template type checking whencustomercan benull. Use?.or guard the block with@if.
Exercise
A component has count: number | undefined = undefined and the template <p>Count: {{ count }}</p>. What text appears, and what does it show after the component runs this.count = 0?
Show answer
At first it shows "Count: " followed by nothing, because undefined renders as an empty string. After the update, change detection re-evaluates the expression and the text becomes "Count: 0". Zero is a real value, so it converts to the string "0".
Exercise
Why does <button disabled="{{ false }}">Save</button> render a disabled button, and how do you fix it?
Show answer
Interpolation converts false to the string "false" and assigns it to the disabled property. A non-empty string is truthy, so the button is disabled. Use property binding instead: <button [disabled]="false">Save</button>.