Component outputs
One key feature of reusable components is to notify the parent component that an action has been performed.
In the AlertBanner component, we want to add a button, triggering a different action based on its usage:
- in the
TaskListcomponent, we want to call a function to delete all tasks - in the
TaskFormcomponent, we want to update the form with basic information
To do so, we’ll use the output feature of Angular.
Define the output
Section titled “Define the output”Instructions
Section titled “Instructions”-
Update the
AlertBannerclass:import { output } from '@angular/core';onSubmit = output<void>(); -
Update the template of
alert-banner.htmlfile:<button class="btn"[class.btn-info]="type() === 'info'"[class.btn-error]="type() === 'error'"(click)="onSubmit.emit()">Proceed</button> -
Update the
TaskListclass:deleteAllTasks() {// TODO: delete all tasks} -
Update the template of
task-list.htmlfile:<app-alert-banner message="Do you really want to delete all tasks?" type="error" (onSubmit)="deleteAllTasks()"/> -
Update the
TaskFormclass:preFillForm() {// TODO: pre fill the form with basic information} -
Update the template of
task-form.htmlfile:<app-alert-banner message="Do you want to prefill Form?" type="info" (onSubmit)="preFillForm()" /> -
Test the application.