Interpolation Binding
Learn how Angular uses interpolation to display component data dynamically inside an HTML template.
What is Interpolation?
Interpolation is an Angular data binding technique used to display component values inside the HTML template.
It uses double curly braces
{{ }}
to evaluate an expression and display its result.
Interpolation Flow
Component Data
Define a property inside the component class.
Expression
Place the property inside interpolation syntax.
Angular
Angular evaluates the interpolation expression.
UI
The evaluated value appears in the template.
Main Interpolation Concepts
Property Value
Display a component property inside the template.
Expression
Evaluate a simple expression and display its result.
Method Call
Call a component method and display its returned value.
Object Property
Access a property from an object stored in the component.
Interpolation Syntax
The basic interpolation syntax uses double curly braces.
{{ expression }}
Angular evaluates the expression between
{{ and }}
and displays the resulting value in the template.
Displaying a Property
The most common use of interpolation is displaying a component property.
export class StudentComponent {
studentName = 'Sam';
}
<h2>
Student Name: {{ studentName }}
</h2>
Student Name: Sam
Displaying Multiple Properties
Multiple component properties can be displayed using interpolation.
export class StudentComponent {
studentName = 'Sam';
course = 'Angular Full Stack';
city = 'Pune';
}
<p>
Name: {{ studentName }}
</p>
<p>
Course: {{ course }}
</p>
<p>
City: {{ city }}
</p>
Name: Sam
Course: Angular Full Stack
City: Pune
Interpolation with Expressions
Angular can evaluate simple expressions inside interpolation.
export class StudentComponent {
firstName = 'Sam';
lastName = 'Patil';
}
<p>
Full Name:
{{ firstName + ' ' + lastName }}
</p>
Full Name: Sam Patil
Arithmetic Expressions
Simple arithmetic expressions can also be evaluated inside interpolation.
export class StudentComponent {
marks1 = 80;
marks2 = 90;
}
<p>
Total Marks:
{{ marks1 + marks2 }}
</p>
Total Marks: 170
Calling a Method
A component method that returns a value can be called from interpolation.
getWelcomeMessage() {
return 'Welcome to CIIT Training Institute!';
}
<h3>
{{ getWelcomeMessage() }}
</h3>
Welcome to CIIT Training Institute!
Object Property Interpolation
Interpolation can access properties from an object.
student = {
name: 'Sam',
course: 'Angular Full Stack',
city: 'Pune'
};
<p>
Student:
{{ student.name }}
</p>
<p>
Course:
{{ student.course }}
</p>
<p>
City:
{{ student.city }}
</p>
Student: Sam
Course: Angular Full Stack
City: Pune
Interpolation Data Flow
Component
studentName = 'Sam'
Interpolation
{{ studentName }}
Angular
Evaluate
UI
Sam
Interpolation vs Property Binding
| Feature | Interpolation | Property Binding |
|---|---|---|
| Syntax | {{ value }} |
[property]="value" |
| Purpose | Display values | Bind element properties |
| Direction | Component → Template | Component → Element |
| Common Usage | Text content | HTML properties |
| Example | {{ studentName }} |
[disabled]="isDisabled" |
Common Uses of Interpolation
Display Text
Display component values inside headings and paragraphs.
Display Object Data
Access and display properties from component objects.
Calculate Values
Evaluate simple expressions and display the result.
Method Results
Display values returned from component methods.
Complete Interpolation Example
export class StudentComponent {
student = {
name: 'Sam',
course: 'Angular Full Stack',
city: 'Pune'
};
}
<div>
<h2>
{{ student.name }}
</h2>
<p>
Course:
{{ student.course }}
</p>
<p>
City:
{{ student.city }}
</p>
</div>
Student Details
Name: Sam
Course: Angular Full Stack
City: Pune
Best Practices
Use simple expressions and avoid complex application logic inside interpolation.
Use interpolation primarily when the result needs to appear as text inside the template.
Avoid methods that perform heavy processing inside interpolation.
Clear property names make templates easier to read and maintain.
Use property binding when you need to bind an actual HTML element property.
Summary
- Interpolation displays component data inside a template.
- It uses double curly braces.
- Simple expressions can be evaluated using interpolation.
- Object properties can be accessed through interpolation.
- Methods that return values can be used when appropriate.
- Interpolation is mainly used for displaying text values.