How To Display Values With Interpolation In Angular?
Last Updated :
28 Aug, 2024
Angular is a powerful framework for building dynamic web applications. One of its key features is data binding, which allows developers to display and update values seamlessly within the UI. Interpolation is a fundamental technique in Angular that enables the integration of component data directly into the HTML templates, making it easier to present dynamic content and computed values.
Prerequisites
Interpolation in Angular
Interpolation in Angular is a powerful feature that allows you to display dynamic data from your component class directly within the HTML template. It uses double curly braces {{ }} to bind the data and render it as part of the DOM. This is commonly used to display variables, expressions, and function results within your template. The syntax is:
Syntax:
{{ component_property }}
Steps to Display Values with Interpolation in Angular
Step 1: Install Angular CLI
If you haven’t installed Angular CLI yet, install it using the following command
npm install -g @angular/cli
Step 2: Create a New Angular Project
ng new interpolation-example
cd interpolation-example
Dependencies
"dependencies": {
"@angular/animations": "^18.1.4",
"@angular/common": "^18.1.4",
"@angular/compiler": "^18.1.4",
"@angular/core": "^18.1.4",
"@angular/forms": "^18.1.4",
"@angular/platform-browser": "^18.1.4",
"@angular/platform-browser-dynamic": "^18.1.4",
"@angular/router": "^18.1.4",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.10"
}
Folder Structure
Folder StructureExample: Create the required files as seen in the folder structure and add the following codes.
In the below example, we are showing how to use Angular's interpolation to display data from the component class directly in the HTML template. The example shows the user's name, age, and email, along with the current date, all bound to the template using double curly braces {{ }}. Additionally, we include an input field that allows the user to change their name, which is instantly reflected on the page through two-way data binding.
App Component
HTML
<!--src\app\app.component.html-->
<div style="text-align: center;">
<h1 style="color: green;">{{ title }}</h1>
<h3>{{ subtitle }}</h3>
<p><strong>Name:</strong> {{ user.name }}</p>
<p><strong>Age:</strong> {{ user.age }}</p>
<p><strong>Email:</strong> {{ user.email }}</p>
<p><strong>Current Date:</strong> {{ currentDate | date:'fullDate' }}</p>
<div>
<input [(ngModel)]="user.name" placeholder="Enter your name" />
<p>Your name is: {{ user.name }}</p>
</div>
</div>
JavaScript
// src\app\app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
JavaScript
//src\app\app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'GeeksforGeeks';
subtitle = 'Displaying values with interpolation in Angular';
user = {
name: 'GFG',
age: 30,
email: '[email protected]'
};
currentDate = new Date();
}
Steps to Run the Application
Open the terminal, run this command from your root directory to start the application
ng serve --open
Open your browser and navigate to http://localhost:4200
Output
Display Values with Interpolation in Angular
Similar Reads
How to display the app version in Angular?
Angular is a client-side TypeScript based, front-end web framework by Google. Angular 8 is a great, reusable UI (User Interface) framework for the developers which help in building Single Page Applications. Generally, all the angular 2+ projects or applications are referred to as Angular application
2 min read
What is Interpolation in AngularJS ?
In this article, we will know about Interpolation in AngularJS, along with understanding the basic implementation. In AngularJS, Interpolation is a way to transfer the data from a TypeScript code to an HTML template (view), i.e. it is a method by which we can put an expression in between some text a
2 min read
How to display static JSON data in table in Angular ?
In this article, we will see How to display static JSON data in the table in Angular. We will be displaying static data objects in Angular Table. We need to iterate over the object keys and values using key values and then handle them in the table. We will be using the bootstrap table and Angular Pr
3 min read
Interpolation vs. Property Binding in Angular
Angular, a powerful front-end framework, offers various techniques to dynamically update and display data in your applications. Two commonly used methods for this purpose are interpolation and property binding. In this article, we will learn about interpolation and property binding and also see the
4 min read
How to Filter Multiple Values in AngularJS ?
AngularJS is one of the popular frameworks of many web developers to create dynamic single-page web applications. To make the application more and more dynamic, we can use the filtering of data feature to dynamically show the data to the user as per the input or selection. These provide a better use
6 min read
How to pass the 2 $index values within nested ng-repeat in AngularJS ?
In AngularJS applications, we can create dynamic behavior by passing the $index values within the nested ng-repeat directives. In the application, the $index variable or value is nothing but the index of the current item or the product that is in the ng-repeat loop. We can pass these values using 2
5 min read
How to use ngFor with index as value in Attribute in Angular ?
NgFor is used as a structural directive that renders each element for the given collection each element can be displayed on the page. In this article, we will learn how to store the index value of ngFor in an attribute so I can utilize it. Creating Angular application & Module Installation Step 1: C
3 min read
How to limit to 2 decimal place with a Angular Pipe ?
Angular Pipes are a way to transform the format of output data for display. The data can be strings, currency amounts, dates, etc. In this article, we will learn How to Limit to 2 decimal places with a Simple Pipe. Steps for Installing & Configuring the Angular Application Step 1: Create an Angular
3 min read
How to use AngularJS to Display HTML content value ?
The ng-bind-html Directive is used to bind the innerHTML of an HTML element to application data and remove dangerous code from the HTML string. The $sanitize service is a must for the ng-bind-html directive. It is supported by all HTML elements. Syntax: <element ng-bind-html="expression"> Cont
2 min read
String Interpolation in Angular 8
String Interpolation in Angular 8 is a One-way Data-Binding technique that is used to transfer the data from a TypeScript code to an HTML template (view). It uses the template expression in double curly braces to display the data from the component to the view. String interpolation adds the value of
2 min read