AI Hints
Home›Angular Tutorial›Components, Templates and Interpolation
Angular · Lesson 21 of 120

Interpolation

Showing values.

⏱ 2 min read▶ Video at 25:29🎓 Free
Watch this lesson in the Angular courseWatch on YouTube ↗Full Angular course

Interpolation means showing a value inside the text, with double curly braces. Whatever is inside the braces is worked out, and shown.

  • Double curly braces
  • Worked out
  • Shown as text

What goes inside {{ }}

A property name, like title. A small expression, like price times two. A method call, like stock message. Keep it short. Bigger logic belongs in the class.

keyvalue
Property{{ title }}
Expression{{ price * 2 }}
Method{{ stockMessage() }}
Keep it shortlogic goes in the class

Values, expressions, and methods

The heading shows the title. The author is shown in upper case. The price for two copies is worked out, and rounded. And stock message is a method, that returns a sentence. Run it. Every value is worked out, and shown as text.

app.tstypescript
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <h2>{{ title }}</h2>
    <p>By {{ author.toUpperCase() }}</p>
    <p>Two copies: £{{ (price * 2).toFixed(2) }}</p>
    <p>{{ stockMessage() }}</p>
  `,
})
export class App {
  title = 'Dune';
  author = 'Frank Herbert';
  price = 9.99;
  stock = 3;
  stockMessage() { return this.stock > 0 ? `Only ${this.stock} left!` : 'Sold out'; }
}

When values change

Interpolation always shows the current value. If the data changes, the text updates. Angular does this best with signals, which tell it exactly what changed. We'll meet signals in module four.

keyvalue
Showsthe current value
Data changestext updates
Best withsignals
Coming upmodule four

Summary

Double curly braces show properties, short expressions, and method results, as text. Next, property binding.

Key takeaways
  • {{ title }}
  • {{ price * 2 }}
  • {{ stockMessage() }}
Next up: Property Binding — [property]="value"