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.
{{ }} to display component data in the template.The basic syntax of interpolation is: {{ expression }}
expression can access component properties and methods.if, for, etc.).Here is a simple Angular component and its template using interpolation to display a user’s name and age.
// 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>
If name = 'Angular Learner' and age = 20, the browser will show:
Angular replaces {{ name }} and {{ age }} with actual values from the component class.
You can use JavaScript-like expressions directly inside {{ }} to perform calculations or call methods.
<!-- 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>
{{ 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.
This example shows more interpolation in a simple “User Information” section, including a random number.
<!-- 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>
Interpolation reads values from the component and calculates new results directly in the template:
age property.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.
{{ }} for one-way data binding from class to template.{{ }} exist in the component class.course = 'Angular' in your component and display: Welcome to {{ course }} course in the template.num = 6) and use interpolation to display its square: {{ num * num }}.{{ new Date() }} and then format it using a method in the component class.isPro = true and show {{ isPro ? 'Pro User' : 'Free User' }} in the UI.