TExcelTAD
Home › Angular › Templates & Interpolation

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:

angular · try it yourself
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.

angular · try it yourself
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:

ts · try it yourself
type Context = Record<string, string | number | null | undefined>;

function escapeHtml(text: string): string {
  return text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}

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: &lt;i&gt;gift&lt;/i&gt; | 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:

angular · try it yourself
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:

html
<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>'?

  1. Hi in italics
  2. The literal text <em>Hi</em>
  3. Nothing
  4. A compile error

Which expression safely reads city when user might be null?

  1. user.city!
  2. user?.city
  3. user->city
  4. user::city

What does @let total = a() + b(); do?

  1. Creates a writable signal
  2. Declares a read-only template variable
  3. Adds a property to the class
  4. Defines a CSS variable