Skip to content

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 TaskList component, we want to call a function to delete all tasks
  • in the TaskForm component, we want to update the form with basic information

To do so, we’ll use the output feature of Angular.

  1. Update the AlertBanner class:

    import { output } from '@angular/core';
    onSubmit = output<void>();
  2. Update the template of alert-banner.html file:

    <button class="btn"
    [class.btn-info]="type() ? type() === 'info' : false"
    [class.btn-error]="type() ? type() === 'error' : false"
    (click)="onSubmit.emit()">Proceed</button>
  3. Update the TaskList class:

    deleteAllTasks() {
    // TODO: delete all tasks
    }
  4. Update the template of task-list.html file:

    <app-alert-banner message="Do you really want to delete all tasks?" type="error" (onSubmit)="deleteAllTasks()"/>
  5. Update the TaskForm class:

    preFillForm() {
    // TODO: pre fill the form with basic information
    }
  6. Update the template of task-form.html file:

    <app-alert-banner message="Do you want to prefill Form?" type="info" (onSubmit)="preFillForm()" />
  7. Test the application.