Communication Between Components in Angular
How Are They Connected?
The TaskListComponent will manage an array of tasks, where each task is an object with id, title, and completed fields. To display each task, we'll use the TaskComponent.
Additionally, TaskComponent will communicate with its parent component about user actions, such as when a task should be deleted or marked as complete. This interaction will happen through event emission.
Implementing the TaskListComponent Logic
Let's create a component that will be responsible for managing the task list. Inside it, we'll define an array of tasks and also add two methods β deleteTask() and toggleStatus() β to handle task management.
task-list.ts
task-list.html
task-list.css
- The list of tasks (
tasks) is managed inside the component, makingTaskListComponentthe central controller; - The
deleteTask()method filters out the task by itsid; - The
toggleStatus()method finds the task byidand toggles itscompletedstatus.
This code uses the *ngFor directive to loop through the tasks array and create a TaskComponent for each task.
We'll take a closer look at how *ngFor works in the next section.
-
[task]="task"β passes the current task to the child component so it can display it; -
(onDelete)="deleteTask($event)"β listens for theonDeleteevent from the child and callsdeleteTask()to remove the task; -
(onToggle)="toggleStatus($event)"β listens for theonToggleevent and callstoggleStatus()to change the task's status.
Decorators: @Input() and @Output()
Now it's time to connect the TaskComponent with the TaskListComponent, and for that, we'll use the decorators @Input() and @Output().
In Angular, the @Input() and @Output() decorators are key tools for communication between components. They allow you to pass data into a component and emit events out of a component. Here's a quick overview:
Here's an example of how they work in the TaskComponent:
task.ts
task.html
task.css
In our case, task is the object passed from the parent TaskListComponent into the TaskComponent.
When the user either deletes a task or toggles its status, the TaskComponent emits events that the parent component listens for.
@Output() and EventEmitter are used to notify the parent component when something happens in the child. These decorators allow the child component to communicate actions like task deletions or status changes to the parent.
The deleteTask() method is called when the user wants to delete a task. It emits the task's id, enabling the parent to remove the task from its list.
The toggleTask() method toggles the task's completed status and informs the parent component about this change.
How It All Works Together
-
TaskListComponentpasses a task toTaskComponentusing@Input(); -
The user interacts with the task (like clicking "Delete");
-
TaskComponentemits an event (onDeleteoronToggle) with the task'sid; -
TaskListComponentcatches the event and updates the task list; -
Angular automatically refreshes the UI based on the updated data.
This way, @Input() and @Output() enable clean, efficient communication between components, keeping the structure organized and reactive.
1. What does the @Input() decorator do in Angular?
2. What is the purpose of EventEmitter in Angular?
3. In TaskComponent, what does the @Output() decorator do?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 3.13
Communication Between Components in Angular
Swipe to show menu
How Are They Connected?
The TaskListComponent will manage an array of tasks, where each task is an object with id, title, and completed fields. To display each task, we'll use the TaskComponent.
Additionally, TaskComponent will communicate with its parent component about user actions, such as when a task should be deleted or marked as complete. This interaction will happen through event emission.
Implementing the TaskListComponent Logic
Let's create a component that will be responsible for managing the task list. Inside it, we'll define an array of tasks and also add two methods β deleteTask() and toggleStatus() β to handle task management.
task-list.ts
task-list.html
task-list.css
- The list of tasks (
tasks) is managed inside the component, makingTaskListComponentthe central controller; - The
deleteTask()method filters out the task by itsid; - The
toggleStatus()method finds the task byidand toggles itscompletedstatus.
This code uses the *ngFor directive to loop through the tasks array and create a TaskComponent for each task.
We'll take a closer look at how *ngFor works in the next section.
-
[task]="task"β passes the current task to the child component so it can display it; -
(onDelete)="deleteTask($event)"β listens for theonDeleteevent from the child and callsdeleteTask()to remove the task; -
(onToggle)="toggleStatus($event)"β listens for theonToggleevent and callstoggleStatus()to change the task's status.
Decorators: @Input() and @Output()
Now it's time to connect the TaskComponent with the TaskListComponent, and for that, we'll use the decorators @Input() and @Output().
In Angular, the @Input() and @Output() decorators are key tools for communication between components. They allow you to pass data into a component and emit events out of a component. Here's a quick overview:
Here's an example of how they work in the TaskComponent:
task.ts
task.html
task.css
In our case, task is the object passed from the parent TaskListComponent into the TaskComponent.
When the user either deletes a task or toggles its status, the TaskComponent emits events that the parent component listens for.
@Output() and EventEmitter are used to notify the parent component when something happens in the child. These decorators allow the child component to communicate actions like task deletions or status changes to the parent.
The deleteTask() method is called when the user wants to delete a task. It emits the task's id, enabling the parent to remove the task from its list.
The toggleTask() method toggles the task's completed status and informs the parent component about this change.
How It All Works Together
-
TaskListComponentpasses a task toTaskComponentusing@Input(); -
The user interacts with the task (like clicking "Delete");
-
TaskComponentemits an event (onDeleteoronToggle) with the task'sid; -
TaskListComponentcatches the event and updates the task list; -
Angular automatically refreshes the UI based on the updated data.
This way, @Input() and @Output() enable clean, efficient communication between components, keeping the structure organized and reactive.
1. What does the @Input() decorator do in Angular?
2. What is the purpose of EventEmitter in Angular?
3. In TaskComponent, what does the @Output() decorator do?
Thanks for your feedback!