Interpolation Explained with Examples - One Way Data Binding
What is Interpolation in Angular?
Interpolation in Angular is a feature used to show TypeScript data in the HTML template. It helps connect the component logic (TypeScript) with the view (HTML).
It is used in almost every real Angular project. If a project does not use interpolation, it means the project is very simple and does not really need Angular.
Basic Concept of Interpolation
When we create an Angular component, two main files are used most of the time:
app.component.ts→ contains logic, properties, functions, calculations, API dataapp.component.html→ used to display data on the browser
Data is always written in the TypeScript file, but it is displayed using the HTML file. Interpolation is the bridge between these two files.
Interpolation Syntax
Interpolation uses double curly braces.
Anything written inside these curly braces is evaluated by Angular.
Example idea:
- A property like
nameis written in the TypeScript file - Using
{{ name }}in HTML will display its value in the browser
If we write just name inside HTML without interpolation, Angular will treat it as normal text and will not display the value.
How Interpolation Works Internally
Angular checks whatever is written inside {{ }} and tries to understand:
- Is it a property from the TypeScript file?
- Is it a function from the component?
- Is it a JavaScript expression (like
10 + 20)?
If Angular finds a valid property or expression, it executes it and displays the output.
If Angular does not find anything valid, it throws an error.
Example understanding:
{{ name }}→ displays property value{{ 10 + 20 }}→ displays calculation result{{ Apple }}→ error (because no such property exists)
Using Multiple Properties with Interpolation
You can display multiple values using interpolation, such as:
- Name
- Any other variable declared in the TypeScript file
Each value must be wrapped inside its own {{ }}.
Using Functions in Interpolation
Interpolation can also call functions defined in the TypeScript file.
Important points:
- Functions must return a value
- Class properties must be accessed using
this - Example use case: when calculation depends on parameters
However, using functions inside interpolation is not recommended unless needed.
Directly using properties is better for performance.
Passing Parameters in Interpolation Functions
You can pass parameters to functions inside interpolation.
Key points:
- Parameters must have proper TypeScript types
- TypeScript requires explicit type definition for parameters
- This is useful when values are dynamic
Example idea:
- Passing two numbers
- Returning their sum
- Displaying the result using interpolation
Limitations of Interpolation
Interpolation has some limitations and cannot be used everywhere.
You can do:
- Display properties
- Call functions
- Write small expressions
- Perform simple calculations
You cannot do:
- Create new variables
- Assign values
- Write complex logic
- Declare properties inside HTML
All heavy logic and assignments should always be done in the TypeScript file, not inside interpolation.
Interpolation as One-Way Data Binding
Interpolation is a form of one-way data binding.
- Data flows from TypeScript → HTML
- If data changes in TypeScript, UI updates automatically
- No manual DOM manipulation is required
If data flows both ways (HTML ↔ TypeScript), it is called two-way data binding, but interpolation supports only one-way binding.
Automatic UI Update
When a property changes due to:
- Button click
- API response
- Any logic in TypeScript
The updated value is automatically reflected in the UI.
No need for:
document.getElementById- Manual refresh
- Extra JavaScript code
Angular handles this internally.
Expressions Inside Interpolation
Interpolation supports expressions like:
- Mathematical calculations
- Simple conditional logic
But:
- Complex logic
- Heavy calculations
- Assignments
should not be written inside interpolation. These should be handled in the TypeScript file.
Security Advantage (XSS Protection)
Interpolation is safe from Cross-Site Scripting (XSS) attacks.
- It helps protect application data
- Prevents malicious scripts from running
- Adds an extra layer of security by default
Inline Template and Interpolation
If a component is very small, you don’t need a separate HTML file.
Instead of templateUrl, you can use:
templateproperty inside the component decorator
Inside an inline template:
- Normal HTML can be written
- Interpolation works exactly the same way
This is commonly asked in interviews.