Templates & Interpolation
A component's template is HTML extended with a small amount of Angular syntax. The simplest addition is interpolation: {{ expression }} evaluates a TypeScript-like expression against the component and inserts the result as text.
Interpolation basics
Anything inside double curly braces is evaluated in the context of the component instance, so you can read fields, call methods and do simple arithmetic:
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-order-summary',
template: `
<h3>Order #{{ orderId }}</h3>
<p>Customer: {{ customer().toUpperCase() }}</p>
<p>Items: {{ quantity() }} × {{ price }} = {{ quantity() * price }} USD</p>
<p>Shipping: {{ quantity() > 2 ? 'free' : '5 USD' }}</p>
<p>Greeting: {{ greet('Lan') }}</p>
`,
})
export class OrderSummary {
orderId = 1042;
price = 12;
customer = signal('Lan Nguyen');
quantity = signal(3);
greet(firstName: string) {
return `Thanks for your order, ${firstName}!`;
}
}What is allowed in an expression?
Template expressions look like TypeScript but are deliberately limited, so templates stay declarative:
| Allowed | Not allowed |
|---|---|
Property access, method calls: user.name, total() |
Assignments: count = 5 (except in event bindings) |
Arithmetic and comparison: a + b, x > 3 |
new, increment/decrement ++ -- |
Ternary ? :, &&, ||, ?? |
Statements: if, for, ; chains |
Optional chaining: user?.address?.city |
Globals like window or console |
Template literals (recent versions): `Hi ${name}` |
Bitwise operators |
Tip: If an expression becomes long, move it into a method or a
computed()signal in the class. Templates should read like a description, not like an algorithm.
Interpolation is always text
Interpolated values are converted to strings and HTML-escaped. That protects you from script injection: a user name containing <b> appears as literal characters, not bold text. null and undefined become empty strings.
import { Component } from '@angular/core';
@Component({
selector: 'app-escape-demo',
template: `
<p>Comment: {{ comment }}</p>
<p>Nickname: "{{ nickname }}"</p>
<p>City: {{ user?.address?.city ?? 'unknown' }}</p>
`,
})
export class EscapeDemo {
comment = '<b>Great course!</b>';
nickname: string | null = null;
user: { address?: { city: string } } | null = null;
}How interpolation works
Conceptually, Angular finds every {{ }} and replaces it with the evaluated, escaped value. Here is a tiny (and much simpler) version written in plain TypeScript — it looks up names in a context object and escapes HTML:
type Context = Record<string, string | number | null | undefined>;
function escapeHtml(text: string): string {
return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
}
function interpolate(template: string, ctx: Context): string {
return template.replace(/\{\{\s*(\w+)\s*\}\}/g, (_match, key: string) => {
const value = ctx[key];
return value == null ? '' : escapeHtml(String(value));
});
}
const ctx: Context = { user: 'Lan', items: 3, note: '<i>gift</i>', coupon: null };
console.log(interpolate('Hi {{ user }}, you have {{items}} items.', ctx));
console.log(interpolate('Note: {{ note }} | Coupon: [{{ coupon }}]', ctx));Hi Lan, you have 3 items.
Note: <i>gift</i> | Coupon: []
Local variables with @let
@let declares a read-only variable inside a template — handy for naming an intermediate value that you use several times:
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-cart-total',
template: `
@let subtotal = price() * qty();
@let tax = subtotal * 0.1;
<p>Subtotal: {{ subtotal }}</p>
<p>Tax: {{ tax }}</p>
<p><strong>Total: {{ subtotal + tax }}</strong></p>
`,
})
export class CartTotal {
price = signal(25);
qty = signal(4);
}Template reference variables
A #name on an element creates a reference you can use elsewhere in the same template — for example to read an input's current value:
<input #search placeholder="Search…" />
<button (click)="find(search.value)">Find</button>Test yourself
What appears on the page for {{ title }} when title is '<em>Hi</em>'?
- Hi in italics
- The literal text <em>Hi</em>
- Nothing
- A compile error
Which expression safely reads city when user might be null?
user.city!user?.cityuser->cityuser::city
What does @let total = a() + b(); do?
- Creates a writable signal
- Declares a read-only template variable
- Adds a property to the class
- Defines a CSS variable