← Back to Chapters

Templates, Interpolation & Expressions

? Templates, Interpolation & Expressions

? Quick Overview

In Angular, templates define the view (UI) of a component. They combine standard HTML with Angular’s own syntax to render dynamic data. Interpolation and expressions are used to bind component data to the template and perform inline calculations directly in the HTML.

Whenever you see {{ expression }} inside the template, Angular evaluates that expression using the component’s data and displays the result in the browser.

? Key Concepts

  • Template – The HTML view connected to a component.
  • Interpolation – Using {{ }} to display component data in the template.
  • Expression – Small JavaScript-like snippets evaluated inside interpolation.
  • One-way Binding – Data flows from the component class to the template (view) only.

? Syntax & Theory

The basic syntax of interpolation is: {{ expression }}

  • expression can access component properties and methods.
  • Only expressions are allowed, not full JavaScript statements (no if, for, etc.).
  • Keep expressions simple; move complex logic into the TypeScript class.

? Basic Interpolation Example

Here is a simple Angular component and its template using interpolation to display a user’s name and age.

? View Code Example
// app.component.ts - basic interpolation
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  name = 'Angular Learner';
  age = 20;
}

// app.component.html - using interpolation
<h1>Hello, {{ name }}!</h1>
<p>Your age is {{ age }}.</p>

?️ Live Output (Conceptual)

If name = 'Angular Learner' and age = 20, the browser will show:

  • Hello, Angular Learner!
  • Your age is 20.

Angular replaces {{ name }} and {{ age }} with actual values from the component class.

? Using Expressions Inside Interpolation

You can use JavaScript-like expressions directly inside {{ }} to perform calculations or call methods.

? View Code Example
<!-- app.component.html - expressions in interpolation -->
<p>Next year, you will be {{ age + 1 }} years old.</p>
<p>Uppercase name: {{ name.toUpperCase() }}</p>
<p>Is adult? {{ age >= 18 ? 'Yes' : 'No' }}</p>

? Explanation

  • {{ age + 1 }} – performs arithmetic using the component property age.
  • {{ name.toUpperCase() }} – calls a string method on name.
  • {{ age >= 18 ? 'Yes' : 'No' }} – uses the ternary operator to show a conditional result.

These are all valid expressions. If they become too complex, move the logic into the TypeScript file as a method and call that method from the template.

? Template Example with Data Binding

This example shows more interpolation in a simple “User Information” section, including a random number.

? View Code Example
<!-- app.component.html - user information template -->
<h2>User Information</h2>
<p>Name: {{ name }}</p>
<p>Age in 5 years: {{ age + 5 }}</p>
<p>Random number (1-10): {{ Math.floor(Math.random() * 10) + 1 }}</p>

? Explanation

Interpolation reads values from the component and calculates new results directly in the template:

  • Age in 5 years – uses arithmetic on the age property.
  • Random number – uses a simple expression to generate a random value between 1 and 10.

Even though this works, it’s often better to move repeated or complex expressions (like random generation) into the component class to keep templates clean and easy to read.

✅ Tips & Best Practices

  • Use interpolation {{ }} for one-way data binding from class to template.
  • Keep templates clean; move business and complex logic into the TypeScript class.
  • Use simple expressions only – no loops, assignments, or complex conditions in the template.
  • Prefer calling lightweight methods or using properties instead of long inline expressions.
  • Always make sure the properties used inside {{ }} exist in the component class.

? Try It Yourself

  • Create a property course = 'Angular' in your component and display: Welcome to {{ course }} course in the template.
  • Add a numeric property (for example, num = 6) and use interpolation to display its square: {{ num * num }}.
  • Show today’s date using {{ new Date() }} and then format it using a method in the component class.
  • Create a property isPro = true and show {{ isPro ? 'Pro User' : 'Free User' }} in the UI.