Course Content
Introduction to Angular
Introduction to Angular
Types of Data Binding in Angular
When a component and its template interact, we can pass data and events between them in different ways. This is called data binding.
In Angular, there are four types of data binding, each serving a specific purpose.
Let's take a look at how they work using a simple task component as an example:
task-component
import { Component } from '@angular/core'; @Component({ selector: 'task-component', standalone: true, templateUrl: './task-component.component.html', styleUrls: ['./task-component.component.css'] }) export class TaskComponent { task = { id: 1, title: 'Buy groceries', completed: false }; }
Here, we create a task object with three properties: id
, title
, and completed
. These properties describe a specific taskβits name and whether it has been completed.
Interpolation
For example, if you have a variable like task.title
, you can wrap it in {{ }}
, and Angular will render its value into the HTML.
python
When Angular processes the template, it finds the expression {{ task.title }}
and replaces it with the current value of task.title
from the component.
If the value is 'Buy groceries', that's exactly what will appear inside the <h3>
tag.
The same process applies to any other values you interpolate.
So, interpolation is one-way: data flows from the component to the template, and the user simply sees the result.
Property Binding
Sometimes, you don't just want to show text β you want to control element behavior, like disabling a button, setting an image source, or checking a checkbox.
Angular lets you bind values to HTML element properties using square brackets.
python
Imagine the task is already completed (task.completed = true
). In that case, we want the button to be disabled. When Angular sees [disabled]="task.completed"
, it takes the value from the component and binds it to the DOM element's disabled
property.
If the value is true
, the button becomes disabled. If it's false
, the button stays active.
Again, this is one-way binding: the data flows from the component to the template, and the template adjusts based on the current state of the task
object.
Event Binding
To make your app respond to user actions (like clicks), Angular provides event binding. When a user interacts with an element, you can "catch" that event and call a method in your component.
Let's add a method to the component:
python
And update the template:
python
When the user clicks the button, Angular detects the (click)
event and calls the toggleComplete()
method in the component. This method toggles the value of task.completed
.
After that, Angular re-renders the template, and thanks to interpolation ({{ task.completed ? 'Undo' : 'Complete' }}
), the button text updates instantly.
Here, data flows one way β from the template back into the component (you pass an action inward).
Two-Way Binding
Sometimes you want changes in the template (like typing into an input field) to immediately update the component β and changes in the component to immediately update the template.
Angular makes this easy with two-way binding using [(ngModel)]
.
Important: To use [(ngModel)]
, you must import the FormsModule
β as shown below:
task-component
import { FormsModule } from '@angular/forms'; @Component({ selector: 'task-component', standalone: true, imports: [FormsModule], // β don't forget to import `FormsModule` for standalone components templateUrl: './task-component.component.html', styleUrls: ['./task-component.component.css'] }) export class TaskComponent { task = { id: 1, title: 'Buy groceries', completed: false }; toggleComplete() { this.task.completed = !this.task.completed; } }
And here's the template:
python
This is where the magic of two-way binding happens.
When a user types into the input field, Angular automatically updates task.title
inside the component. And when task.title
changes in the component, Angular immediately reflects that update in the input field.
Unlike the other types of binding, two-way binding keeps the component and the template in sync at all times.
Key Differences
Each of these binding methods has its own features and is best suited for different scenarios of data interaction within an application.
1. Which type of data binding is used to display the value of a component's variable in the template?
2. Which type of data binding allows you to update a component's variable through a form and keeps it synchronized with the input element in the template?
3. Which type of data binding would you use to make a button disabled when a task is completed?
4. Which type of data binding allows you to trigger a component method when a button is clicked?
Thanks for your feedback!