Inputs carry data down to a child. Outputs carry events back up to the parent. This part covers output(), emit, $event, model() for two-way binding, and a cart with a quantity stepper.
In Part 5, data went one way: down. A parent bound values to a child’s inputs, and the child could read them but not set them. That’s half a conversation. A delete button inside a todo item, a star rating, a quantity picker: each of these lives in a child, and each needs to tell the parent that something happened.
This part covers output() and emit, listening with (name)="handler($event)", how to name an output, and model(), which gives you two-way binding with [( )]. It builds on signals from Part 3 and inputs from Part 5. Every example here is a whole file you can paste into your practice project from Part 1. Each one was built with Angular 22.3 and run in a browser before this was published.
Try this first
At the end of Part 5, a Counter child couldn’t reset its own count, because the count was an input. Here the child gets a Reset button anyway. Make src/app/counter.ts and paste the first file. Paste the second into src/app/app.ts.
// src/app/counter.ts
import { Component, input, output } from '@angular/core';
@Component({
selector: 'app-counter',
template: `
<p>Count: {{ count() }}</p>
<button (click)="resetRequested.emit()">Reset</button>
`,
})
export class Counter {
readonly count = input.required<number>();
readonly resetRequested = output<void>();
}
// src/app/app.ts
import { Component, signal } from '@angular/core';
import { Counter } from './counter';
@Component({
selector: 'app-root',
imports: [Counter],
template: `
<app-counter [count]="count()" (resetRequested)="count.set(0)" />
<button (click)="addOne()">Add one</button>
`,
})
export class App {
protected readonly count = signal(0);
protected addOne() {
this.count.update((n) => n + 1);
}
}
Before you read on, guess. Click Add one three times, then Reset. The Reset button is in the child, and the child can’t set count. Does the count go back to 0?
Click Add one three times. The page shows:
Count: 3
ResetAdd one
Now click Reset. The page shows:
Count: 0
ResetAdd one
It worked, and the child never touched count. (ResetAdd one is the two buttons side by side: buttons sit on one line, and the page text runs them together.) It sent a message up: “reset was asked for”. The parent heard it, and the parent set its own signal to 0. The new value then came back down through [count], as in Part 5. That round trip is what this part is about.
output(): an event the child sends up
The child declares the event with output():
readonly resetRequested = output<void>();
output() comes from @angular/core. The type in angle brackets says what the event carries. void means “nothing”: this event only says that something happened. (output() with no type means the same thing, void.) The field is readonly, like inputs and signals, and public by convention, because it’s part of what the component offers its parents.
To send the event, the child calls emit(). Our child does it right in its template: (click)="resetRequested.emit()". Angular calls sending an event emitting it.
The parent listens with the same round brackets it uses for a click: (resetRequested)="count.set(0)". To the parent’s template, the child’s output looks just like a DOM event. When the child emits, Angular runs the parent’s statement, and because it’s an event in the parent’s template, Angular redraws the parent afterwards.
Sending a value with the event
Most events carry something: which star was picked, which item to delete, what was typed. Give the output a type, and pass the value to emit:
// src/app/star-rating.ts
import { Component, output } from '@angular/core';
@Component({
selector: 'app-star-rating',
template: `
<button (click)="rated.emit(1)">1 star</button>
<button (click)="rated.emit(2)">2 stars</button>
<button (click)="rated.emit(3)">3 stars</button>
`,
})
export class StarRating {
readonly rated = output<number>();
}
// src/app/app.ts
import { Component, signal } from '@angular/core';
import { StarRating } from './star-rating';
@Component({
selector: 'app-root',
imports: [StarRating],
template: `
<app-star-rating (rated)="setRating($event)" />
<p>Your rating: {{ rating() }}</p>
`,
})
export class App {
protected readonly rating = signal(0);
protected setRating(stars: number) {
this.rating.set(stars);
}
}
Click 2 stars. The page shows:
1 star2 stars3 stars
Your rating: 2
In Part 2, $event was the browser’s event object, like a PointerEvent for a click. On a component’s output, $event is whatever the child passed to emit. Here that’s the number 2.
$event has the output’s type
output<number>() makes a promise, and the build holds both sides to it. Say the parent’s handler expects a string by mistake:
// src/app/star-rating.ts
import { Component, output } from '@angular/core';
@Component({
selector: 'app-star-rating',
template: `
<button (click)="rated.emit(1)">1 star</button>
<button (click)="rated.emit(2)">2 stars</button>
<button (click)="rated.emit(3)">3 stars</button>
`,
})
export class StarRating {
readonly rated = output<number>();
}
// src/app/app.ts
import { Component, signal } from '@angular/core';
import { StarRating } from './star-rating';
@Component({
selector: 'app-root',
imports: [StarRating],
template: `
<app-star-rating (rated)="setRating($event)" />
<p>Your rating: {{ rating() }}</p>
`,
})
export class App {
protected readonly rating = signal('none');
protected setRating(stars: string) {
this.rating.set(stars);
}
}
✘ [ERROR] TS2345: Argument of type 'number' is not assignable to parameter of type 'string'. [plugin angular-compiler]
TS2345 is TypeScript’s code for “this argument doesn’t fit the parameter”. The argument is $event, a number, and setRating wants a string. The error points at $event in the parent’s template. Angular type-checks event bindings the same way it checks input bindings.
The other side is checked too. If the child emits text on a number output, this.rated.emit('three') fails with the same code the other way round: Argument of type 'string' is not assignable to parameter of type 'number', pointing at the emit call in the child.
Naming an output
Angular’s guide on angular.dev gives four rules for output names, and each one has a reason.
- Name what happened, in camelCase.
rated,removed,resetRequested,quantityChange. The parent decides what to do about it; the child just reports. - Don’t start with
on. Write(removed)="remove()", not(onRemoved). The round brackets already say “on”. (Older code often hasonSomethingoutputs.) - Don’t add a prefix the way you would to a selector. The selector is
app-star-rating; the output israted, notappRated. - Don’t reuse a DOM event name.
click,change,input,toggle,resetandsubmitare all events that real elements fire.
The last rule matters more than it looks. Here a child has an output called click:
// src/app/child.ts
import { Component, output } from '@angular/core';
@Component({
selector: 'app-child',
template: `<button (click)="go()">Go</button>`,
})
export class Child {
readonly click = output<string>();
protected go() {
this.click.emit('from output');
}
}
// src/app/app.ts
import { Component } from '@angular/core';
import { Child } from './child';
@Component({
selector: 'app-root',
imports: [Child],
template: `<app-child (click)="log($event)" />`,
})
export class App {
protected log(value: unknown) {
console.log(String(value));
}
}
Click Go. The console shows:
from output
[object PointerEvent]
The handler ran twice. Once for the output, with the string. Once for the browser’s own click, which bubbled up from the button to the <app-child> element. The build said nothing about it. Angular 22’s compiler has no check for output names that clash with DOM events, or for an on prefix. If you add the angular-eslint linter to your project, its no-output-native and no-output-on-prefix rules catch both. Without it, those are conventions you keep yourself.
The handler runs twice only for DOM events that bubble up through the page, like click, input, change and submit. Some, like toggle, don’t bubble. The rule still stands: a reader of (toggle) can’t tell which one you meant.
alias
output() takes an options object with one option, alias, the name the parent uses:
// src/app/panel.ts
import { Component, output } from '@angular/core';
@Component({
selector: 'app-panel',
template: `<button (click)="closed.emit()">Close</button>`,
})
export class Panel {
readonly closed = output<void>({ alias: 'panelClosed' });
}
// src/app/app.ts
import { Component, signal } from '@angular/core';
import { Panel } from './panel';
@Component({
selector: 'app-root',
imports: [Panel],
template: `
@if (open()) {
<app-panel (panelClosed)="open.set(false)" />
} @else {
<p>Panel closed</p>
}
`,
})
export class App {
protected readonly open = signal(true);
}
Click Close. The page shows:
Panel closed
The class emits closed; the parent listens to panelClosed. As with input aliases in Part 5, angular.dev advises against aliases in your own components. They’re for keeping an old public name working after a rename, or for giving the parent a name that doesn’t clash with a DOM event while the class keeps a short one.
The parent owns the state
Look back at the counter. Two components show the count, but only one of them owns it: App, which holds the signal. The child has a copy it can read (its input) and a way to ask for a change (its output). It never changes the count itself.
This is called unidirectional data flow: one-way data flow. Data goes down through inputs. Events go up through outputs. Each piece of state has one owner, and only the owner writes to it. When something looks wrong on the screen, you know where to look: the owner’s signal and the methods that change it.
Here’s the round trip from the start of this part, one click at a time. Solid black arrows are data going down; dashed red arrows are events going up.
An event goes up, the parent’s signal changes, and the new value comes back down.
Here are those steps in words, in case the animation doesn’t play for you:
Appholdscountat 2 and binds it to the Counter with[count]="count()". The Counter showsCount: 2.- You click Reset in the Counter. It emits
resetRequested, and the event goes up toApp. Nothing has changed yet: the child can’t write the count, only ask. App‘s handler runscount.set(0). The owner changes its own signal. For a moment, the parent holds 0 and the child still shows 2.- Angular redraws. The binding
[count]="count()"now gives 0, the Counter’s input changes, and the Counter showsCount: 0.
Worked example: a todo list
A todo item is a good child component. It shows one todo, and it has two things a user can do: tick it and remove it. The list itself belongs to the parent. So the item gets the todo as an input and reports the two actions as outputs.
// src/app/todo-item.ts
import { Component, input, output } from '@angular/core';
export interface Todo {
id: number;
text: string;
done: boolean;
}
@Component({
selector: 'app-todo-item',
template: `
<label>
<input type="checkbox" [checked]="todo().done" (change)="toggled.emit()" />
{{ todo().text }}
</label>
<button (click)="removed.emit()">Remove</button>
`,
})
export class TodoItem {
readonly todo = input.required<Todo>();
readonly toggled = output<void>();
readonly removed = output<void>();
}
// src/app/app.ts
import { Component, computed, signal } from '@angular/core';
import { Todo, TodoItem } from './todo-item';
@Component({
selector: 'app-root',
imports: [TodoItem],
template: `
@for (todo of todos(); track todo.id) {
<p><app-todo-item [todo]="todo" (toggled)="toggle(todo.id)" (removed)="remove(todo.id)" /></p>
} @empty {
<p>Nothing to do.</p>
}
<p>{{ left() }} left</p>
`,
})
export class App {
protected readonly todos = signal<Todo[]>([
{ id: 1, text: 'Buy milk', done: false },
{ id: 2, text: 'Call Ana', done: false },
{ id: 3, text: 'Water plants', done: true },
]);
protected readonly left = computed(() => this.todos().filter((t) => !t.done).length);
protected toggle(id: number) {
this.todos.update((list) => list.map((t) => (t.id === id ? { ...t, done: !t.done } : t)));
}
protected remove(id: number) {
this.todos.update((list) => list.filter((t) => t.id !== id));
}
}
Some things to notice:
- Both outputs are
void. The child doesn’t send the todo’sid, because the parent already knows which todo it is. Inside the@for,(removed)="remove(todo.id)"uses the loop’stodo. Each item’s listener is tied to its own todo. toggledandremoveddescribe what happened.togglewould clash with a DOM event of that name, andremovereads like a command.- The child’s checkbox shows
todo().donewith[checked], and the(change)event on the checkbox turns into atoggledoutput. The child doesn’t changedoneitself. It can’t:todois an input. toggleandremovefollow Part 3: they make a new list, andtogglemakes a new object for the todo that changed. That gives the item a new input value, so it redraws.
The page shows:
Buy milk Remove
Call Ana Remove
Water plants Remove
2 left
Tick Buy milk. The page shows:
Buy milk Remove
Call Ana Remove
Water plants Remove
1 left
Now click the first Remove, which is the one next to Buy milk. The page shows:
Call Ana Remove
Water plants Remove
1 left
The count didn’t change, because Buy milk was already done. Every change went through App‘s two methods. If the count were ever wrong, those two methods are the only place to look.
model(): two-way binding
Some children edit a value the parent holds: a quantity picker, a toggle switch, a text box. With what we have so far, that’s an input for the value plus an output for changes, and the parent writing the new value back. It’s common enough that Angular has a shortcut: model().
// src/app/stepper.ts
import { Component, model } from '@angular/core';
@Component({
selector: 'app-stepper',
template: `<button (click)="value.update((v) => v + 1)">Add one</button>`,
})
export class Stepper {
readonly value = model(0);
}
// src/app/app.ts
import { Component, signal } from '@angular/core';
import { Stepper } from './stepper';
@Component({
selector: 'app-root',
imports: [Stepper],
template: `
<app-stepper [(value)]="quantity" />
<p>Quantity: {{ quantity() }}</p>
`,
})
export class App {
protected readonly quantity = signal(1);
}
Click Add one twice. The page shows:
Add one
Quantity: 3
Three things are new:
model(0)declares a model input. It’s an input, so the parent can pass a value in, and the 0 is its default. Unlikeinput(), it’s also a writable signal: the child can callsetandupdateon it.- When the child writes to it, the model sends the new value up as an output named
valueChange: the field name plusChange. [(value)]="quantity"binds both ways at once. People call[( )]the banana in a box: round brackets (the banana) inside square ones (the box). Square outside, round inside.
Notice that the parent passes quantity, the signal itself, not quantity(). A two-way binding needs something it can write back to, and a signal’s value isn’t that. We’ll see the errors for getting this wrong in a worked example below.
If you swap the brackets by mistake, the build still succeeds, but ng serve prints an NG8101 warning:
// src/app/app.ts
import { Component, signal } from '@angular/core';
import { Stepper } from './stepper';
@Component({
selector: 'app-root',
imports: [Stepper],
template: `
<app-stepper ([value])="(quantity)" />
<p>Quantity: {{ quantity() }}</p>
`,
})
export class App {
protected readonly quantity = signal(1);
}
In the two-way binding syntax the parentheses should be inside the brackets
Click Add one, and the page shows:
Add one
Quantity: 1
The binding does nothing, and the quantity stays at 1. (The value reads "(quantity)" above because that’s how prettier formats this mistake when you save.) Read the ▲ [WARNING] lines, and put the round brackets inside.
What [( )] expands to
[(value)]="quantity" is short for a property binding and an event binding together. This parent does exactly the same thing, written out by hand:
// src/app/app.ts
import { Component, signal } from '@angular/core';
import { Stepper } from './stepper';
@Component({
selector: 'app-root',
imports: [Stepper],
template: `
<app-stepper [value]="quantity()" (valueChange)="quantity.set($event)" />
<p>Quantity: {{ quantity() }}</p>
`,
})
export class App {
protected readonly quantity = signal(1);
}
Click Add one twice. The page shows:
Add one
Quantity: 3
So model() isn’t a new kind of data flow. It’s an input going down and an output coming up, with the parent writing the new value into its signal. The parent still owns the value. Two details matter:
valueChangefires only when the child writes a new value to the model. Setting the value it already holds emits nothing, as with any signal. When the parent changesquantityitself, the new value goes down to the child, and novalueChangecomes back up. (We checked with aconsole.login the handler.)- If you use the long form, you need both halves. With only
[value]="quantity()", the child’s model still changes when it writes, so the child and the parent drift apart. Exercise 3 shows how that goes wrong.
A plain field works too
[( )] also accepts a plain class field. Angular reads it for the input and assigns to it for the output:
// src/app/stepper.ts
import { Component, model } from '@angular/core';
@Component({
selector: 'app-stepper',
template: `<button (click)="value.update((v) => v + 1)">Add one</button>`,
})
export class Stepper {
readonly value = model(0);
}
// src/app/app.ts
import { Component } from '@angular/core';
import { Stepper } from './stepper';
@Component({
selector: 'app-root',
imports: [Stepper],
template: `
<app-stepper [(value)]="quantity" />
<p>Quantity: {{ quantity }}</p>
`,
})
export class App {
protected quantity = 1;
}
Click Add one twice. The page shows:
Add one
Quantity: 3
It works because the change arrives as an event in App‘s template, and an event marks App for a redraw. But it has the plain-field problem from Part 3: if anything else changes quantity, like a timer, the screen won’t hear about it. Bind a signal.
model.required and alias
model.required<number>() works like input.required: there’s no default, so you give the type, and a parent that doesn’t bind it gets the build error NG8008, Required input 'value' from component Stepper must be specified. model() also takes alias. Then the output is named after the alias: model(0, { alias: 'count' }) gives an input count and an output countChange.
Worked example: a quantity stepper in a cart
Now a stepper you’d actually use. It has – and + buttons, a lowest and a highest value, and it disables a button at the limit. The cart has two lines, each with its own stepper, and a total.
// src/app/quantity-stepper.ts
import { Component, input, model } from '@angular/core';
@Component({
selector: 'app-quantity-stepper',
template: `
<button
(click)="decrease()"
[disabled]="value() <= min()"
[attr.aria-label]="'One less ' + label()"
>
-
</button>
{{ value() }}
<button
(click)="increase()"
[disabled]="value() >= max()"
[attr.aria-label]="'One more ' + label()"
>
+
</button>
`,
})
export class QuantityStepper {
readonly value = model.required<number>();
readonly label = input.required<string>();
readonly min = input(0);
readonly max = input(10);
protected decrease() {
this.value.update((v) => Math.max(this.min(), v - 1));
}
protected increase() {
this.value.update((v) => Math.min(this.max(), v + 1));
}
}
// src/app/app.ts
import { Component, computed, signal } from '@angular/core';
import { QuantityStepper } from './quantity-stepper';
@Component({
selector: 'app-root',
imports: [QuantityStepper],
template: `
<p>Tea, 4 dollars: <app-quantity-stepper label="Tea" [max]="3" [(value)]="tea" /></p>
<p>Bread, 3 dollars: <app-quantity-stepper label="Bread" [(value)]="bread" /></p>
<p>Total: {{ total() }} dollars</p>
<button (click)="clear()">Clear cart</button>
`,
})
export class App {
protected readonly tea = signal(1);
protected readonly bread = signal(0);
protected readonly total = computed(() => this.tea() * 4 + this.bread() * 3);
protected clear() {
this.tea.set(0);
this.bread.set(0);
}
}
Some things to notice:
- The stepper mixes a model and plain inputs.
valuechanges both ways.label,minandmaxonly come down. - The handlers clamp the value with
Math.maxandMath.min, so a value never goes past a limit, and the disabled buttons make the limit visible. - Each button shows only
-or+, which doesn’t say what it changes.[attr.aria-label]from Part 2 gives each one a real name, like “One more Tea”. Screen readers read that name, and it lets us tell the two steppers’ buttons apart. totalis computed from the two signals the steppers write to. The cart never asks a stepper for its value.- Clear cart is in the parent and sets the parent’s signals. The new values flow down into the steppers. Both directions go through the same signals.
The page shows:
Tea, 4 dollars: - 1 +
Bread, 3 dollars: - 0 +
Total: 4 dollars
Clear cart
Click + next to Tea twice, and + next to Bread once. The page shows:
Tea, 4 dollars: - 3 +
Bread, 3 dollars: - 1 +
Total: 15 dollars
Clear cart
Tea is now at its max of 3, so its + button is disabled. Click Clear cart. The page shows:
Tea, 4 dollars: - 0 +
Bread, 3 dollars: - 0 +
Total: 0 dollars
Clear cart
Worked example: two-way binding to the wrong thing
[( )] needs something it can write to. Here are the two mistakes people make, with what the build says. The cart above has a total, and someone binds a stepper straight to it:
// src/app/stepper.ts
import { Component, model } from '@angular/core';
@Component({
selector: 'app-stepper',
template: `<button (click)="value.update((v) => v + 1)">Add one</button>`,
})
export class Stepper {
readonly value = model(0);
}
// src/app/app.ts
import { Component, computed, signal } from '@angular/core';
import { Stepper } from './stepper';
@Component({
selector: 'app-root',
imports: [Stepper],
template: `<app-stepper [(value)]="total" />`,
})
export class App {
protected readonly quantity = signal(1);
protected readonly total = computed(() => this.quantity() * 4);
}
✘ [ERROR] TS2322: Type 'Signal<number>' is not assignable to type 'number'. [plugin angular-compiler]
8 │ template: `<app-stepper [(value)]="total" />`,
Read it piece by piece:
TS2322is “this value doesn’t fit this type”, the same code asprice="5"in Part 5.Type 'Signal<number>'is the type oftotal. A computed is a read-onlySignal, as Part 3 showed. A two-way binding accepts aWritableSignal, which it can callseton, or a plain value it can assign to. A read-onlySignalis neither, so Angular checks it as a plain value, and aSignal<number>isn’t anumber.- The line points at the whole
[(value)]="total"binding in the parent.
The message doesn’t say “you can’t write to a computed”, but that’s what it means. The fix is to bind the stepper to quantity, the signal that total is computed from.
The second mistake is adding the (), since that’s how you read a signal everywhere else:
// src/app/app.ts
import { Component, signal } from '@angular/core';
import { Stepper } from './stepper';
@Component({
selector: 'app-root',
imports: [Stepper],
template: `<app-stepper [(value)]="quantity()" />`,
})
export class App {
protected readonly quantity = signal(1);
}
✘ [ERROR] NG5002: Unsupported expression in a two-way binding [plugin angular-compiler]
8 │ template: `<app-stepper [(value)]="quantity()" />`,
NG5002 comes from Angular’s template parser. quantity() is a function call that gives a number. You can’t assign a new value to a function call, so Angular refuses it before type checking even starts. In a two-way binding, write the signal without (). Everywhere else, keep the ().
Here’s the fixed version:
// src/app/app.ts
import { Component, computed, signal } from '@angular/core';
import { Stepper } from './stepper';
@Component({
selector: 'app-root',
imports: [Stepper],
template: `
<app-stepper [(value)]="quantity" />
<p>{{ quantity() }} cups, {{ total() }} dollars</p>
`,
})
export class App {
protected readonly quantity = signal(1);
protected readonly total = computed(() => this.quantity() * 4);
}
Click Add one. The page shows:
Add one
2 cups, 8 dollars
In older code
Before output(), an output was a field with the @Output() decorator holding an EventEmitter: @Output() removed = new EventEmitter<void>();, emitted the same way with this.removed.emit(). EventEmitter is built on an RxJS Subject, so you’ll sometimes see code that subscribes to it. Two-way binding was an @Input() value and an @Output() valueChange = new EventEmitter<number>() written by hand, with the child calling this.valueChange.emit(...). The Change suffix is why [(value)] still looks for valueChange today. model() replaces the pair. (For outputs built from an RxJS stream, there’s outputFromObservable; Part 20 covers RxJS.)
Best practices
- Keep each piece of state in one owner, and let children ask. The owner holds the signal and the methods that change it. A bug in the state then has one place to look. This replaces children that keep their own copy and hope it stays in step.
- Use outputs for events and
model()for a value the child edits. A delete button is an event:output(). A quantity picker edits a number:model(). It replaces an input plus a hand-writtenvalueChangeoutput. - Give outputs the type they carry, and
voidwhen they carry nothing. The build then checks the child’semitand the parent’s handler. It replaces$any($event)and handlers that guess. - Name outputs for what happened, without
on, and never after a DOM event.removed,rated,resetRequested. It avoids handlers that run twice for one click. - Bind
[( )]to a writable signal, without(). Not a computed, not a call. The parent’s screen then updates however the value changes.
Tips and traps
- A typo in the output name.
(remvoed)="remove()"builds without a word and does nothing, because Angular treats an unknown name as a DOM event that never fires. Check the spelling against the child’s class. (Angular has a compiler option,strictUnclaimedEventNames, that a new project leaves off. Turned on, it only catches names with a capital letter in them.) - An output named after a DOM event.
clickorchangeon a component makes the parent’s handler run for the output and again for the browser event when it bubbles up from inside the child, asclickandchangedo, and the second time$eventisn’t the type you declared. Rename the output. - The wrong type on either side. TS2345 at the child’s
emitor at$eventin the parent’s template. Make the output’s type, the value emitted and the handler’s parameter agree. [(value)]="quantity()". NG5002,Unsupported expression in a two-way binding. Drop the().[(value)]="total"wheretotalis a computed. TS2322,Type 'Signal<number>' is not assignable to type 'number'. Bind the writable signal thattotalis computed from.[value]without(valueChange)on a model. The child changes its own copy, and the parent doesn’t hear. Then setting the parent’s signal to the value it already holds does nothing, and the child stays out of step. Use[(value)].
Data goes down through inputs, events come up through outputs, and only the owner of a signal writes to it.
Exercises
Exercise 1. Clicking Remove on this contact card should hide it. Nothing happens, and the build shows no error. Why? Fix it.
// src/app/contact-card.ts
import { Component, output } from '@angular/core';
@Component({
selector: 'app-contact-card',
template: `
<p>Ana Kim</p>
<button (click)="removed.emit()">Remove</button>
`,
})
export class ContactCard {
readonly removed = output<void>();
}
// src/app/app.ts
import { Component, signal } from '@angular/core';
import { ContactCard } from './contact-card';
@Component({
selector: 'app-root',
imports: [ContactCard],
template: `
@if (shown()) {
<app-contact-card (remove)="shown.set(false)" />
} @else {
<p>Removed</p>
}
`,
})
export class App {
protected readonly shown = signal(true);
}
Ana Kim
Remove
Exercise 2. Write a Toggle component in src/app/toggle.ts with a checked model that defaults to false, and a button labelled Switch that flips it. In App, bind it two ways to a darkMode signal and show Dark mode: on or Dark mode: off. After one click on Switch, the page should show Dark mode: on.
Exercise 3. Without running it, what does this page show after you click Add one twice? And after you then click Reset? Hint: Part 3 showed what set does when the value doesn’t change.
// src/app/stepper.ts
import { Component, model } from '@angular/core';
@Component({
selector: 'app-stepper',
template: `<button (click)="value.update((v) => v + 1)">Add one</button> {{ value() }}`,
})
export class Stepper {
readonly value = model(0);
}
// src/app/app.ts
import { Component, signal } from '@angular/core';
import { Stepper } from './stepper';
@Component({
selector: 'app-root',
imports: [Stepper],
template: `
<app-stepper [value]="quantity()" />
<p>Parent: {{ quantity() }}</p>
<button (click)="quantity.set(1)">Reset</button>
`,
})
export class App {
protected readonly quantity = signal(1);
}
Add one 3
Parent: 1
Reset
Add one 3
Parent: 1
Reset
Answers
1. The child’s output is removed, and the parent listens to (remove). No output has that name, so Angular adds a listener for a DOM event called remove on <app-contact-card>, and nothing ever fires one. The build can’t tell a typo from a custom DOM event. Use the output’s real name:
// src/app/contact-card.ts
import { Component, output } from '@angular/core';
@Component({
selector: 'app-contact-card',
template: `
<p>Ana Kim</p>
<button (click)="removed.emit()">Remove</button>
`,
})
export class ContactCard {
readonly removed = output<void>();
}
// src/app/app.ts
import { Component, signal } from '@angular/core';
import { ContactCard } from './contact-card';
@Component({
selector: 'app-root',
imports: [ContactCard],
template: `
@if (shown()) {
<app-contact-card (removed)="shown.set(false)" />
} @else {
<p>Removed</p>
}
`,
})
export class App {
protected readonly shown = signal(true);
}
Click Remove. The page shows:
Removed
2. The child flips its model with update, and the parent binds its signal with [(checked)]:
// src/app/toggle.ts
import { Component, model } from '@angular/core';
@Component({
selector: 'app-toggle',
template: `<button (click)="checked.update((c) => !c)">Switch</button>`,
})
export class Toggle {
readonly checked = model(false);
}
// src/app/app.ts
import { Component, signal } from '@angular/core';
import { Toggle } from './toggle';
@Component({
selector: 'app-root',
imports: [Toggle],
template: `
<app-toggle [(checked)]="darkMode" />
<p>Dark mode: {{ darkMode() ? 'on' : 'off' }}</p>
`,
})
export class App {
protected readonly darkMode = signal(false);
}
Click Switch. The page shows:
Switch
Dark mode: on
3. After two clicks, the child shows 3 and the parent still shows 1. The child’s model is writable, so update changed the child’s copy and emitted valueChange. Nobody listened, so quantity stayed at 1. Then Reset calls quantity.set(1). quantity is already 1, so the signal doesn’t change, the binding has nothing new to pass down, and the child keeps showing 3. So the page shows Add one 3 and Parent: 1, before and after Reset.
The fix is [(value)]="quantity". Then both clicks write to quantity, the parent shows 3, and Reset really changes it back to 1:
// src/app/stepper.ts
import { Component, model } from '@angular/core';
@Component({
selector: 'app-stepper',
template: `<button (click)="value.update((v) => v + 1)">Add one</button> {{ value() }}`,
})
export class Stepper {
readonly value = model(0);
}
// src/app/app.ts
import { Component, signal } from '@angular/core';
import { Stepper } from './stepper';
@Component({
selector: 'app-root',
imports: [Stepper],
template: `
<app-stepper [(value)]="quantity" />
<p>Parent: {{ quantity() }}</p>
<button (click)="quantity.set(1)">Reset</button>
`,
})
export class App {
protected readonly quantity = signal(1);
}
Click Add one twice. The page shows:
Add one 3
Parent: 3
Reset
Click Reset, and it shows:
Add one 1
Parent: 1
Reset
What’s next
Our components now talk both ways, but they all look the same: plain text and default buttons. In Part 7, we’ll give each component its own styles, see how Angular keeps them from leaking into other components, and use :host and class bindings.