What is Interpolation in AngularJS ?
Last Updated :
26 Aug, 2022
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 and get the value of that expression. Interpolation basically binds the text with the expression value. Interpolation is a one-way data binding. Through this, we can bind some data values from our logic. To use the interpolation we need to put our expression into the double curly braces {{ }}. With Interpolation, only string data/ expression will be evaluated, ie., only string parameter will be used in the interpolation.
Syntax:
{{ expression }}
Working of Interpolation: During the compilation process, the $interpolate service is being utilized by the compiler in order to display if the text & the attribute have interpolation markup, along with the embedded expressions. For this case, an interpolateDirective will be added by the compiler to the text node and registers watches on the computed interpolation function, for which the respective text node or the attribute values will be updated, which is part of the normal digest cycle.
We can pass different parameters, ie., a boolean value, arithmetic operation, numeric value, etc, in the form of expression into the double curly braces {{ }}, that will transform into the string data.
Example: This example describes the basic usage of the Interpolation in AngularJS.
app.component.html
<div>{{3+2}}</div>
<div>{{title}}</div>
<div>{{value}}</div>
<div>{{bool}}</div>
app.component.ts
import { Component } from "@angular/core";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"],
})
export class AppComponent {
title: string = "GeeksforGeeks";
value: number = 0;
bool: boolean = true;
}
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({
imports: [BrowserModule, FormsModule],
declarations: [AppComponent],
bootstrap: [AppComponent],
})
export class AppModule {}
Output:
In the above example, we have used different expressions and data binding as:
- Expression interpolation
- String interpolation
- Number Value interpolation
- Boolean Value interpolation
Benefits of the Interpolation:
- We can use the expression and display its value directly in HTML.
- We can bind data from our typescript logic.
- We can render the variable and it will keep updating values.
- We can have one-way data binding.
Similar Reads
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
What is currency filter in AngularJS ? In this article, we will know the currency filters in AngularJS, along with understanding its implementation through the examples. Filters are used to modify or format the given data in a certain way. AngularJS has different filters such as uppercase, lowercase, etc. One of those filters is the curr
2 min read
How To Display Values With Interpolation In Angular? 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 i
3 min read
Introduction to AngularJS AngularJS is a popular open-source framework that simplifies web development by creating interactive single-page applications (SPAs). Unlike traditional websites that load new pages for each click, SPAs offer a smoother user experience by updating content on the same page. AngularJS makes this possi
4 min read
What is Angular Expression ? Angular is a great, reusable UI (User Interface) library for developers that help in building attractive, and steady web pages and web application. In this article, we will learn about Angular expression. Table of Content Angular ExpressionDifferent Use Cases of Angular ExpressionsSyntaxApproach Ang
4 min read
What is scope in AngularJS ? In this article, we will see what the scope is in AngularJS and how to use Scope, along with understanding its implementation through the examples. The Scope in AngularJS is the binding part between HTML (view) and JavaScript (controller) and it is a built-in object. It contains application data and
4 min read