Templates and Interpolation

Double braces in a template, {{ like this }}, show a value from the component's class. They can hold any simple expression, and a pipe after a | formats it, such as uppercase, currency or date, once the pipe is imported.

Novice · Angular · Updated

After this lesson you can

  • Show a component's data in its template with interpolation, and format it with pipes

Key takeaways

  • {{ companyName }} in a template shows a property of the component's class, by name, with no this..
  • Make a property the template shows protected: its own template can use it, other classes can't.
  • Double braces can hold a simple expression, such as companyName.length; anything longer belongs in the class as a property or method.
  • A pipe after a | formats a value, such as {{ price | currency }} for $18.00, and must be in the component's imports, such as CurrencyPipe.

A template can show the component's data. Put a property in the class, and write its name in double braces in the template. That's interpolation, like @Model.Name in Razor.

Showing data

header.ts:

import { Component } from '@angular/core';

@Component({
  imports: [],
  selector: 'app-header',
  styleUrl: './header.css',
  templateUrl: './header.html',
})
export class Header {
  protected readonly companyName = 'Northwind Traders';
  protected readonly tagline = 'Fine foods from around the world';
}

header.html:

<header class="bg-dark text-white p-3 mb-4">
  <h1 class="h3 m-0">{{ companyName }}</h1>
  <p class="m-0">{{ tagline }}</p>
</header>

The page shows Northwind Traders and Fine foods from around the world.

  • The template can use the class's properties by name, with no this..
  • protected makes a property visible to the component's own template, but not to other classes, which is just right for data a template shows. (readonly says it never changes, like C#'s.)

Expressions

Between the braces can go any simple expression:

<p>{{ companyName.length }} letters</p>
<p>{{ 2 + 3 }}</p>
<p>{{ companyName.toUpperCase() }}</p>
<p>{{ isOpen ? 'Open now' : 'Closed' }}</p>

With isOpen = true in the class, those show 17 letters, 5, NORTHWIND TRADERS and Open now.

Keep them simple: a property, a small calculation, a call to a method. Anything longer belongs in the class, as a property or method of its own.

Pipes

A pipe formats a value for display. Write it after a |, and give it options after a :. Pipes must be imported, like components:

import { Component } from '@angular/core';
import { CurrencyPipe, DatePipe, UpperCasePipe } from '@angular/common';

@Component({
  imports: [CurrencyPipe, DatePipe, UpperCasePipe],
  selector: 'app-special-offer',
  template: `
    <p>{{ productName | uppercase }}</p>
    <p>{{ price | currency }}</p>
    <p>Until {{ endsOn | date: 'longDate' }}</p>
  `,
})
export class SpecialOffer {
  protected readonly productName = 'Chai';
  protected readonly price = 18;
  protected readonly endsOn = new Date(2026, 11, 31);
}
CHAI
$18.00
Until December 31, 2026

Notice template instead of templateUrl: for a small component, the HTML can sit in the class, between backticks. (JavaScript's Date counts months from 0, so 11 is December.)

Pipe Shows price = 18 / endsOn / 'Chai' as Like in C#
uppercase, lowercase CHAI, chai ToUpper(), ToLower()
currency $18.00 :C
number: '1.2-2' 18.00 :0.00
date: 'longDate' December 31, 2026 :MMMM d, yyyy

Check yourself

1 question to try on the lesson page.

So what

Double braces show the class's data, and pipes dress it up for people to read. The class holds the data, the template shows it, and Angular keeps the two in step.

Key terms

Cheat sheets