Sub topic 9
Parent-Child Data Flow with @Input and @Output
This chapter assumes you already know property binding and event binding from earlier chapters, and that you can create standalone components with the Angular CLI. It uses those same two binding forms. This time, though, you bind to your own components instead of to native DOM elements.
The Working Model
Data flows down and events flow up. The parent passes values to the child through inputs, which are properties marked with @Input(). The child reports back to the parent through outputs, which are EventEmitter properties marked with @Output(). The parent binds to an input with [name]="expr", the same way it would bind a DOM property. It listens to an output with (name)="handler($event)", the same way it would listen to a DOM event.
A Complete Example
The child shows a button. When the user clicks it, the child emits its own id.
import { Component, EventEmitter, Input, Output } from '@angular/core';
@Component({
selector: 'app-item',
template: `<button (click)="remove.emit(id)">Remove {{ id }}</button>`,
})
export class ItemComponent {
@Input() id = 0;
@Output() remove = new EventEmitter<number>();
}The parent renders one child per id and removes an entry when a child emits:
import { Component } from '@angular/core';
import { ItemComponent } from './item.component';
@Component({
selector: 'app-list',
imports: [ItemComponent],
template: `
@for (itemId of ids; track itemId) {
<app-item [id]="itemId" (remove)="onRemove($event)" />
}
<p>{{ ids.length }} items left</p>
`,
})
export class ListComponent {
ids = [3, 7, 12];
onRemove(id: number) {
this.ids = this.ids.filter(x => x !== id);
}
}For a custom output, $event is exactly the value passed to emit(). In this example it is a number. It is not a DOM Event.
Useful Options
| Option | Syntax | Effect |
|---|---|---|
| Required input | `@Input({ required: true }) id!: number;` | Compile error if the parent omits it |
| Alias | `@Input('itemId') id = 0;` | Parent binds `[itemId]`, while the class uses `id` |
| Transform | `@Input({ transform: booleanAttribute }) disabled = false;` | Lets `<app-item disabled />` become `true` |
booleanAttribute and numberAttribute are imported from @angular/core.
Common Errors
- Forgetting to import the child. If
ItemComponentis missing from the parent'simports, you get "'app-item' is not a known element" or "Can't bind to 'id'". - Static versus bound values.
id="7"passes the string"7".[id]="7"evaluates the expression and passes the number7. - Mutating an input object. If the child changes a field on an object it received, it silently changes the parent's data. Emit an output and let the parent own the change.
- Expecting an input to update the child's local copy. If the child copies an input into another field once, in the constructor, that copy never updates. Read the input directly. If the child needs to react when an input changes, use
ngOnChangesor an input setter. - Naming an output after a native event. An output called
clickfires alongside the DOM'sclickevent, which bubbles up from inside the child. Use a specific name such asremoveorselected. - Importing
EventEmitterfrom the wrong place. It comes from@angular/core, not from Node'seventsmodule.
Exercise
A parent template contains <app-item id="5" (remove)="onRemove($event.target)" />. Name the two bugs and fix the binding.
Show answer
id="5" passes the string "5" instead of a number, so use [id]="5". For a custom output, $event is the emitted value (the number id), not a DOM Event, so $event.target is undefined. The fix is <app-item [id]="5" (remove)="onRemove($event)" />.
Exercise
Add a required input named label to ItemComponent so the button reads "Remove <label>". What happens if the parent leaves it out?
Show answer
Declare @Input({ required: true }) label!: string; and change the template to Remove {{ label }}. If the parent omits [label] or label="...", the Angular compiler reports an error that the required input 'label' is missing.