Data Binding Introduction
Learn how Angular connects component data with the HTML template using different types of data binding.
What is Data Binding?
Data binding is the mechanism Angular uses to connect the component class with the template.
It allows data to move between the component and the user interface so that the displayed content can update dynamically.
Data Binding Flow
Component
Component contains application data and logic.
Binding
Angular connects the component with the template.
Template
Template displays or sends data through bindings.
UI
The user interacts with the rendered interface.
Main Types of Data Binding
Interpolation
Displays component data inside the HTML template.
Property Binding
Binds component data to an element property.
Event Binding
Sends user events from the template to the component.
Two-Way Binding
Synchronizes component data and user input.
Component to Template
Data can move from the component class to the template using interpolation and property binding.
export class StudentComponent {
studentName = 'Sam';
course = 'Angular Full Stack';
}
<h2>
Hello {{ studentName }}
</h2>
<p>
Course: {{ course }}
</p>
Hello Sam
Course: Angular Full Stack
Interpolation Binding
Interpolation uses double curly braces to display component properties inside the template.
<h2>
Welcome {{ studentName }}
</h2>
Welcome Sam
Property Binding
Property binding connects a component property with an HTML element property.
<button [disabled]="isDisabled">
Submit
</button>
Here Angular evaluates isDisabled from the
component and assigns the result to the button's
disabled property.
Event Binding
Event binding allows the template to respond to user actions such as button clicks.
<button (click)="showMessage()">
Click Me
</button>
showMessage() {
console.log('Button clicked');
}
Two-Way Data Binding
Two-way data binding keeps the component property and the form control value synchronized.
<input
[(ngModel)]="studentName"
placeholder="Enter student name">
Component
studentName
Template
input
User
Types Name
One-Way Data Binding
In one-way binding, data moves in one direction between the component and template.
Component
studentName
Template
{{ studentName }}
UI
Sam
Data Binding Types
| Binding Type | Syntax | Data Direction | Purpose |
|---|---|---|---|
| Interpolation | {{ value }} |
Component → Template | Display data |
| Property Binding | [property] |
Component → Template | Set element properties |
| Event Binding | (event) |
Template → Component | Handle user events |
| Two-Way Binding | [(ngModel)] |
Both directions | Synchronize data |
Common Uses of Data Binding
Display Data
Show component values dynamically in the UI.
Update Properties
Bind values to HTML element properties.
Handle Events
Respond to user interactions.
Form Input
Synchronize user input with component data.
Complete Data Binding Example
export class StudentComponent {
studentName = 'Sam';
course = 'Angular Full Stack';
showMessage() {
console.log(
'Student: ' + this.studentName
);
}
}
<h2>
Student: {{ studentName }}
</h2>
<p>
Course: {{ course }}
</p>
<button
(click)="showMessage()">
Show Student
</button>
Student Details
Student: Sam
Course: Angular Full Stack
Best Practices
Know whether data is moving from component to template, template to component, or in both directions.
Use interpolation, property binding, event binding, and two-way binding for their appropriate purposes.
Complex application logic should remain inside the component or service.
Use two-way binding for scenarios where input changes should update component data.
Clear names make Angular templates easier to understand and maintain.
Summary
- Data binding connects Angular components with templates.
- Interpolation displays component data.
- Property binding updates element properties.
- Event binding handles user interactions.
- Two-way binding synchronizes component and template data.
- Data binding makes Angular applications dynamic and interactive.