Interpolation refers to embedding expressions into marked up text. By default, interpolation uses the double curly braces {{
and }}
as delimiters.
To illustrate how interpolation works, consider an Angular component that contains a currentCustomer
variable:
import {Component} from '@angular/core';import {NgFor} from '@angular/common';import {CUSTOMERS} from './customers';@Component({ standalone: true, selector: 'app-root', templateUrl: './app.component.html', imports: [NgFor], styleUrls: ['./app.component.css'],})export class AppComponent { customers = CUSTOMERS; currentCustomer = 'Maria'; title = 'Featured product:'; itemImageUrl = '../assets/potted-plant.svg'; recommended = 'You might also like:'; itemImageUrl2 = '../assets/lamp.svg'; getVal(): number { return 2; }}
Use interpolation to display the value of this variable in the corresponding component template:
<div> <h1>Interpolation and Template Expressions</h1> <hr /> <div> <h2>Interpolation</h2> <h3>Current customer: {{ currentCustomer }}</h3> <p>{{ title }}</p> <div><img alt="item" src="{{ itemImageUrl }}"></div> <h3>Evaluating template expressions </h3> <h4>Simple evaluation (to a string):</h4> <!-- "The sum of 1 + 1 is 2" --> <p>The sum of 1 + 1 is {{ 1 + 1 }}.</p> <h4>Evaluates using a method (also evaluates to a string):</h4> <!-- "The sum of 1 + 1 is not 4" --> <p>The sum of 1 + 1 is not {{ 1 + 1 + getVal() }}.</p> </div> <hr /><h2>Expression Context</h2><div> <h3>Component context, properties of app.component.ts:</h3> <h4>{{ recommended }}</h4> <img alt="item 2" [src]="itemImageUrl2"></div><div> <h4>Template context, template input variables (let customer):</h4> <ul> @for (customer of customers; track customer) { <li>{{ customer.name }}</li> } </ul></div><div (keyup)="0"> <h4>Template context: template reference variables (#customerInput):</h4> <label for="customer-input">Type something: <input id="customer-input" #customerInput>{{ customerInput.value }} </label></div></div>
Angular replaces currentCustomer
with the string value of the corresponding component property. In this case, the value is Maria
.
In the following example, Angular evaluates the title
and itemImageUrl
properties to display some title text and an image.
<div> <h1>Interpolation and Template Expressions</h1> <hr /> <div> <h2>Interpolation</h2> <h3>Current customer: {{ currentCustomer }}</h3> <p>{{ title }}</p> <div><img alt="item" src="{{ itemImageUrl }}"></div> <h3>Evaluating template expressions </h3> <h4>Simple evaluation (to a string):</h4> <!-- "The sum of 1 + 1 is 2" --> <p>The sum of 1 + 1 is {{ 1 + 1 }}.</p> <h4>Evaluates using a method (also evaluates to a string):</h4> <!-- "The sum of 1 + 1 is not 4" --> <p>The sum of 1 + 1 is not {{ 1 + 1 + getVal() }}.</p> </div> <hr /><h2>Expression Context</h2><div> <h3>Component context, properties of app.component.ts:</h3> <h4>{{ recommended }}</h4> <img alt="item 2" [src]="itemImageUrl2"></div><div> <h4>Template context, template input variables (let customer):</h4> <ul> @for (customer of customers; track customer) { <li>{{ customer.name }}</li> } </ul></div><div (keyup)="0"> <h4>Template context: template reference variables (#customerInput):</h4> <label for="customer-input">Type something: <input id="customer-input" #customerInput>{{ customerInput.value }} </label></div></div>