Sending Messages with output()
A child component declares the messages it can send with output(), and sends one with emit. The parent listens with an event binding on the child's tag, and $event is the value that was sent, so the parent decides what happens.
Journeyman · Angular · Updated
After this lesson you can
- Declare a component's outputs with output(), and send a message with emit
- Handle a child component's message in the parent with an event binding and $event
Key takeaways
output<Category>()declares a message a child can send, andthis.chosen.emit(value)sends it with a value.- The parent listens with an event binding on the child's tag, such as
(chosen)="onChosen($event)", and$eventis the value that was emitted. - Inputs carry data down and outputs carry messages up. The child only reports what happened; the parent, which owns the data, decides what to do.
Data flows down through inputs. Messages flow up through outputs: a child says "this happened", and the parent decides what to do about it. It's how a button tells you it was clicked, in your own components.
Declaring and sending an output
The category card gets a Shop button, and an output for when it's clicked:
import { Component, input, output } from '@angular/core';
export interface Category {
categoryId: number;
categoryName: string;
}
@Component({
imports: [],
selector: 'app-category-card',
template: `
<div class="card">
<div class="card-body">
<h2 class="h5 card-title">{{ category().categoryName }}</h2>
<button class="btn btn-primary btn-sm" (click)="shop()">Shop {{ category().categoryName }}</button>
</div>
</div>
`,
})
export class CategoryCard {
readonly category = input.required<Category>();
readonly chosen = output<Category>();
protected shop() {
this.chosen.emit(this.category());
}
}
output<Category>()declares an output namedchosen, whose messages carry aCategory.this.chosen.emit(value)sends a message, with a value.
The card doesn't know what "shop" should do: show the products, open a page, log something. That's the parent's business. The card only reports what happened.
Listening in the parent
The parent listens with an event binding on the child's tag, exactly as it would for (click). $event is the value that was emitted:
import { Component, signal } from '@angular/core';
import { Category, CategoryCard } from './category-card';
@Component({
imports: [CategoryCard],
selector: 'app-category-picker',
template: `
@for (category of categories(); track category.categoryId) {
<app-category-card [category]="category" (chosen)="onChosen($event)" />
}
<p class="mt-3">Chosen: {{ chosenName() }}</p>
`,
})
export class CategoryPicker {
protected readonly categories = signal<Category[]>([
{ categoryId: 1, categoryName: 'Beverages' },
{ categoryId: 2, categoryName: 'Condiments' }
]);
protected readonly chosenName = signal('nothing yet');
protected onChosen(category: Category) {
this.chosenName.set(category.categoryName);
}
}
The page starts with Chosen: nothing yet. Click Shop Condiments, and it says Chosen: Condiments.
- The button's
(click)runsshop()in the card. shop()emits the card's category onchosen.- The parent's
(chosen)binding runsonChosen($event), with that category. - The parent updates its signal, and the page.
Down and up
| Direction | Child declares | Parent writes | Like |
|---|---|---|---|
| Parent to child: data | category = input.required<Category>() |
[category]="..." |
Giving a constructor its parameters |
| Child to parent: messages | chosen = output<Category>() |
(chosen)="onChosen($event)" |
An event the parent subscribes to |
The child never reaches up to change the parent's data, and the parent never reaches into the child. Each talks through the other's inputs and outputs, so either can be changed without breaking the other: the same idea as an interface.
Check yourself
2 questions to try on the lesson page.
So what
Inputs down, outputs up. A child reports what happened with emit, and the parent, which owns the data, decides what it means.