Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Components  ›  Lesson

Parent and Child: Inputs and Outputs

Parent-Child Interaction 15 minA parent passes data down into a child through signal inputs, and the child reports events back up through outputs that the parent listens to.

Parent and Child: Inputs and Outputs

PlantList holds all the plants. Each PlantCard shows one and has a Water button. The card needs its plant from the list, and the list needs to hear when the button is pressed. Angular gives each direction its own tool.

The rule

Data flows down through inputs; events flow up through outputs.

The child declares both ends:

import { Component, input, output } from '@angular/core';

@Component({
  selector: 'app-plant-card',
  template: `
    <h3>{{ plant().name }}</h3>
    <button (click)="water()">Water</button>
  `,
})
export class PlantCard {
  plant = input.required<Plant>();   // data coming down
  compact = input(false);            // optional, with a default
  watered = output<number>();        // events going up

  water() {
    this.watered.emit(this.plant().id);
  }
}
  • input() creates a read-only signal the parent fills. input.required<T>() means the parent must supply it; input(false) has a default.
  • output<T>() creates an event the child fires with emit(value).

The parent binds both on the tag:

@for (p of plants(); track p.id) {
  <app-plant-card [plant]="p" (watered)="markWatered($event)" />
}

[plant]="p" sends data down. (watered)="..." listens, and $event is whatever the child emitted, here the plant's id.

An input signal has no set method. The child cannot overwrite what it was given. That is the point: the component that owns the data is the only one that changes it, so you always know where to look.

Older code uses decorators for the same job, @Input() plant!: Plant; and @Output() watered = new EventEmitter<number>();. The idea is identical.

The shortcut

Before wiring anything, draw an arrow. Does the value travel from parent to child? Square brackets and input(). Does news travel from child to parent? Round brackets and output(). Then ask who owns the data: the owner updates it after hearing the event.

Wiring the Water buttonStep 1 of 4
  1. Draw the arrows

    The plant goes down (list to card). "I was watered" goes up (card to list).

Drills

Answer fast — trust the rule.

quick drill

How does PlantList pass a plant into PlantCard?

quick drill

Inside PlantCard, what fires the watered event?

quick drill

In (watered)="markWatered($event)", what is $event?

quick drill

Why can't PlantCard call this.plant.set(newPlant)?

Remember

  • Data down with [x] and input(); events up with (y) and output().
  • Shortcut: draw the arrow, then let the owner of the data change it.
  • Trap: trying to change an input inside the child instead of emitting.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWho changes the plant?

PlantList owns the plant array. PlantCard shows one plant and has a Water button. Describe the input and output you would give PlantCard, how PlantList uses them, and why the card should not change the plant itself.