Sub topic 8
Passing Data Between Components with Inputs and Outputs
Components are only useful in a tree if they can talk to each other. Angular gives you one channel in each direction. Inputs carry data down from parent to child. Outputs carry events up from child to parent. This chapter covers the signal-based input() and output() functions, their options, and the older @Input and @Output decorators you will still see in existing codebases.
This chapter assumes you already know property binding ([prop]="expr"), event binding ((event)="handler($event)"), reading signals in templates, and how a standalone component lists its dependencies in imports.
The Data Flow Model
A parent sets a child's input by binding to it like a DOM property. The child never reaches up into the parent. To send something back, the child emits an output, and the parent listens to it like a DOM event. Data moves one way at a time, so you can trace any value to the one place that owns it.
The parent owns the state. The child shows it and asks for changes, and the parent decides whether to apply them.
Declaring Inputs with input()
input() creates a read-only signal on the child. Angular writes to it whenever the parent's bound expression changes, and you read it by calling it.
import {
Component,
booleanAttribute,
computed,
input,
numberAttribute,
output
} from '@angular/core';
@Component({
selector: 'app-star-rating',
template: `
@for (star of stars(); track $index) {
<button type="button" [disabled]="locked()" (click)="select($index + 1)">
{{ value() > $index ? '★' : '☆' }}
</button>
}
`
})
export class StarRatingComponent {
value = input.required<number>();
max = input(5, { transform: numberAttribute });
locked = input(false, { transform: booleanAttribute });
rated = output<number>();
stars = computed(() => Array.from({ length: this.max() }));
select(n: number) {
this.rated.emit(n);
}
}The inputs here show the three common forms:
input.required<number>()has no default. The compiler reports an error if a parent uses<app-star-rating>without bindingvalue.input(5, ...)is optional and falls back to5. Its type is inferred from the default.input(false, { transform: booleanAttribute })runs a transform function on every incoming value before storing it.
Inputs are signals, so derived values belong in computed(). stars recomputes only when max changes. You don't need a lifecycle hook to react to input changes.
Transforms
Static attributes in HTML are always strings. Without a transform, max="10" would put the string "10" into a number input, and the compiler would reject it. numberAttribute and booleanAttribute from @angular/core cover the usual cases. booleanAttribute treats a bare attribute (locked, which arrives as "") as true, and "false" as false.
You can write your own transform. It must be a pure function, because Angular may call it on every change:
import { Component, input } from '@angular/core';
function trimmed(value: string | undefined): string {
return (value ?? '').trim();
}
@Component({
selector: 'app-label',
template: `<span>{{ text() }}</span>`
})
export class LabelComponent {
text = input('', { transform: trimmed });
}Aliases
The alias option changes the public name that parents bind to, while the class keeps its own name:
import { Component, input } from '@angular/core';
@Component({
selector: 'app-badge',
template: `<span>{{ badgeText() }}</span>`
})
export class BadgeComponent {
badgeText = input('', { alias: 'label' });
}
// Parent: <app-badge label="New" />Use aliases rarely. They make the code harder to search, because the template name and the class name no longer match.
Declaring Outputs with output()
output<T>() returns an OutputEmitterRef<T>. Call .emit(value) to fire it. The emitted value reaches the parent as $event, typed as T. Outputs accept an alias option too: output<number>({ alias: 'change' }).
Here is the parent that uses the rating component:
import { Component, signal } from '@angular/core';
import { StarRatingComponent } from './star-rating.component';
@Component({
selector: 'app-review',
imports: [StarRatingComponent],
template: `
<app-star-rating [value]="score()" max="10" (rated)="score.set($event)" />
<app-star-rating [value]="3" locked />
<p>Your score: {{ score() }}</p>
`
})
export class ReviewComponent {
score = signal(0);
}The first rating is interactive. The child emits a number, and the parent stores it. The new score then flows back down through [value]. The second rating is fixed at 3 and locked through the bare locked attribute.
Outputs do not bubble through the DOM. Only the direct parent's template can listen to (rated). If an ancestor further up needs the event, each component in between must re-emit it, or you share state through a service.
The Decorator Forms
Code written before Angular 17.1 uses decorators. They behave the same in templates, but the class side is different:
import { Component, EventEmitter, Input, Output } from '@angular/core';
@Component({
selector: 'app-legacy-todo',
template: `
<label>
<input type="checkbox" [checked]="done" (change)="toggled.emit(!done)" />
{{ title }}
</label>
`
})
export class LegacyTodoComponent {
@Input({ required: true }) title!: string;
@Input({ transform: booleanAttribute }) done = false;
@Output() toggled = new EventEmitter<boolean>();
}booleanAttribute must also be imported from @angular/core for this to compile. Add it to the import line when you copy the example.
| Aspect | `input()` / `output()` | `@Input` / `@Output` |
|---|---|---|
| Reading the value | Call it: `title()` | Plain property: `title` |
| Writable inside the child | No, read-only signal | Yes, which leads to bugs |
| Reacting to changes | `computed()` or `effect()` | `ngOnChanges` or a setter |
| Required inputs | `input.required<T>()` | `@Input({ required: true })` plus `!` |
| Emitter type | `OutputEmitterRef<T>` | `EventEmitter<T>` (an RxJS Subject) |
| Works with OnPush and zoneless | Natively | Yes, with fewer guarantees |
For new code, use the functions. Migrate old code when you touch it. The Angular CLI ships schematics for this: ng generate @angular/core:signal-input-migration and ng generate @angular/core:output-migration.
Common Errors
Not importing the child. If you use <app-star-rating> without adding StarRatingComponent to imports, you get "is not a known element".
Binding without brackets. value="score()" passes the literal string "score()". Write [value]="score()" to evaluate the expression.
Trying to set an input. this.value.set(4) does not compile, because InputSignal has no set method. If the child needs a local copy that it can change, derive one with linkedSignal(() => this.value()). If it needs to write back to the parent, use model(), which the next chapter covers.
Reading a required input too early. Calling this.value() in the constructor throws error NG0950, because Angular has not set the input yet. Read inputs in templates, in computed(), or in ngOnInit and later hooks.
Mutating objects you received. If a parent passes an object and the child changes this.item().done = true, it modifies the parent's state without the parent knowing. Emit an output and let the parent create a new object instead.
Forgetting the parentheses. {{ value }} with a signal input renders the signal function itself, not its value.
Exercises
Exercise
A parent writes <app-star-rating [value]="2" max="7" locked="false" />. What are the values of value(), max(), and locked() inside the child, and why?
Show answer
value() is the number 2, because the bracket binding evaluates the expression 2. max() is the number 7, because numberAttribute converts the static string "7". locked() is false, because booleanAttribute treats the string "false" as false. Any other value, including the empty string from a bare attribute, gives true.
Exercise
Write a QuantityPicker component with a required quantity number input, an optional step input that defaults to 1 and accepts static string attributes, and an output quantityChange that emits the new value when a "+" or "-" button is clicked. Never emit a value below zero.
Show answer
import { Component, input, numberAttribute, output } from '@angular/core'; @Component({ selector: 'app-quantity-picker', template: <button type="button" (click)="change(-step())">-</button> {{ quantity() }} <button type="button" (click)="change(step())">+</button> }) export class QuantityPickerComponent { quantity = input.required<number>(); step = input(1, { transform: numberAttribute }); quantityChange = output<number>(); change(delta: number) { this.quantityChange.emit(Math.max(0, this.quantity() + delta)); } } The parent holds the value in a signal and binds it with [quantity]="qty()" (quantityChange)="qty.set($event)".
Exercise
A grandparent wants to know when a star is clicked inside <app-star-rating>, which sits inside <app-review>. It adds (rated)="log($event)" to <app-review>, and the handler never runs. Explain why and give two fixes.
Show answer
Outputs do not bubble. Only the template that renders app-star-rating can listen to (rated), and app-review has no output named rated. Fix 1: declare rated = output<number>() on ReviewComponent and re-emit inside its handler, for example (rated)="score.set($event); rated.emit($event)". Fix 2: move the score into an injectable service that both components inject.