Sub topic 6
Declaring outputs with output()
An input sends data down from a parent component to a child. An output sends an event up from the child to the parent. This chapter covers the output() function, how to emit typed events, and how the parent receives them.
This chapter assumes you know TypeScript classes and generics, Angular standalone components, and the basics of property binding [prop] and event binding (event). You should also know how to read a signal by calling it. The earlier chapters on input() apply here too.
What an output is
An output is a named event that a component exposes. The parent listens to it in its template, the same way it listens to a DOM event such as click. The child decides when the event fires and what value it carries. The child does not know who is listening, or whether anyone is.
You declare an output by calling output() in a class field initializer:
import { Component, input, output } from '@angular/core';
@Component({
selector: 'app-counter',
template: `
<button (click)="increment()">+{{ step() }}</button>
<span>{{ count }}</span>
`,
})
export class Counter {
step = input(1);
changed = output<number>();
count = 0;
increment() {
this.count += this.step();
this.changed.emit(this.count);
}
}Three things happen here:
output<number>()creates anOutputEmitterRef<number>. The type argument is the type of the event payload.- The field name,
changed, is the name the parent uses in its template. emit()sends a value to every listener. TypeScript rejectsemit('five')because the payload must be anumber.
The event flow
Handling the output in the parent
In the parent template, you use event binding with the output name. The expression $event holds the value passed to emit().
<app-counter [step]="5" (changed)="onChanged($event)" />The parent class defines the handler:
import { Component } from '@angular/core';
import { Counter } from './counter';
@Component({
selector: 'app-root',
imports: [Counter],
template: `
<app-counter [step]="5" (changed)="onChanged($event)" />
<p>Last value: {{ last }}</p>
`,
})
export class App {
last = 0;
onChanged(value: number) {
this.last = value;
}
}Angular type-checks the binding. If onChanged accepted a string, a strict template build would report an error, because $event is a number.
You can also write a small expression instead of calling a method:
<app-counter (changed)="last = $event" />Inline expressions are fine for one assignment. Move to a method once the handler needs more than one step.
Outputs without a payload
Some events carry no data, such as "closed" or "dismissed". Call output() without a type argument and call emit() without an argument:
import { Component, output } from '@angular/core';
@Component({
selector: 'app-dialog',
template: `
<div class="dialog">
<ng-content />
<button (click)="close()">Close</button>
</div>
`,
})
export class Dialog {
closed = output();
close() {
this.closed.emit();
}
}The parent does not need $event:
<app-dialog (closed)="showDialog = false">Are you sure?</app-dialog>Emitting objects
When an event needs several values, emit one object with a named type instead of several arguments. emit() accepts exactly one value.
import { Component, input, output } from '@angular/core';
export interface QuantityChange {
productId: string;
quantity: number;
}
@Component({
selector: 'app-quantity-picker',
template: `
<button (click)="change(-1)">-</button>
<button (click)="change(1)">+</button>
`,
})
export class QuantityPicker {
productId = input.required<string>();
quantity = input(1);
quantityChange = output<QuantityChange>();
change(delta: number) {
const next = Math.max(0, this.quantity() + delta);
this.quantityChange.emit({ productId: this.productId(), quantity: next });
}
}Notice that the child does not modify its own quantity input. It reports the requested change, and the parent decides what to do. This keeps the parent as the single owner of the data.
Aliasing an output
By default, the property name is the event name. If you want a different public name, pass an alias:
import { Component, output } from '@angular/core';
@Component({
selector: 'app-search-field',
template: `<input (input)="submitted.emit('query')" />`,
})
export class SearchField {
submitted = output<string>({ alias: 'search' });
}The parent writes (search)="run($event)", but inside the class you still use this.submitted. Use aliases sparingly. A second name for the same thing makes code harder to search.
Listening from TypeScript
Besides template binding, you can subscribe to an output in code. OutputEmitterRef has a subscribe() method that returns an object with an unsubscribe() method. This is useful when you create components dynamically.
import { Component, EnvironmentInjector, createComponent, inject } from '@angular/core';
import { Counter } from './counter';
@Component({
selector: 'app-dynamic-host',
template: ``,
})
export class DynamicHost {
private injector = inject(EnvironmentInjector);
create() {
const ref = createComponent(Counter, { environmentInjector: this.injector });
const subscription = ref.instance.changed.subscribe((value) => {
console.log('Counter changed to', value);
});
return { ref, subscription };
}
}Angular cleans up template-bound listeners when the component is destroyed. A subscription you create yourself is your responsibility only if the emitter outlives the listener. When the child is destroyed, its output stops working, so a plain subscribe() on that child's own output does not leak it.
How output() differs from @Output and EventEmitter
The older approach declared @Output() changed = new EventEmitter<number>(). The new function is similar for parents, but there are differences inside the class.
| Aspect | `@Output()` with `EventEmitter` | `output()` |
|---|---|---|
| Declaration | Decorator plus `new EventEmitter<T>()` | `output<T>()` function call |
| Is an RxJS `Subject` | Yes, it extends `Subject` | No, it is a separate `OutputEmitterRef` |
| Has `pipe()`, `next()` | Yes | No |
| Emit method | `emit()` | `emit()` |
| Listening in code | `subscribe()` | `subscribe()` |
| Public name | Decorator argument | `alias` option |
The practical consequence is that you cannot call pipe() on an output(). If you need RxJS operators, convert it with outputToObservable() from @angular/core/rxjs-interop. A later chapter covers this.
Edge cases and pitfalls
- Emitting after destruction. Once the component is destroyed, its outputs are closed. Calling
emit()afterward throws error NG0953. This can happen when an async callback, such as a timer or an HTTP response, finishes after the user has left the page. Cancel such work when the component is destroyed, or check that it is still alive before emitting. - Emitting too early. Listeners from the parent template are attached while the child is created. An
emit()in the field initializer or constructor may run before the parent is listening. Emit in response to user actions, lifecycle hooks, or effects, not during construction. - Native event names. An output called
clickorinputworks, but a parent's(click)binding then reacts to both your output and the real DOM event. Pick distinctive names such asselectedorquantityChange. - No bubbling. Component outputs do not bubble up the DOM tree like native events. Only the direct parent template that binds the output sees it. To reach a grandparent, the middle component needs its own output and must re-emit.
- The
onprefix. Do not name outputsonChanged. The Angular style guide recommends names that describe the event, such aschanged, because the template already adds the parentheses that mark it as an event. Reserveonfor the parent's handler methods. - Mutating the payload.
emit()passes the object by reference. If the parent changes the object, the child's copy changes too. Emit a new object or a read-only value when the child keeps its own copy.
Naming conventions summary
- Use a noun or past-tense verb for the event:
closed,selected,quantityChange. - Use camelCase, and do not use dashes in names.
- Type every payload. Prefer a named interface over inline object types once the payload has more than one field.
- Keep the handler in the parent short, and put logic in methods.
Exercise
A Rating component has an input value = input(0). You want it to report when the user picks a star from 1 to 5, and the parent should read the star number in its handler. Write the output declaration and the line of the click handler that emits it, assuming the handler receives star: number.
Show answer
Declare picked = output<number>();, then call this.picked.emit(star); inside the handler. The parent binds it with (picked)="onPicked($event)", where $event is the star number. The child does not change its own value input. The parent updates the value it passes down.
Exercise
A component calls this.saved.emit(result) inside the callback of a timer that was started in ngOnInit. The user navigates away before the timer fires, and the console shows error NG0953. Why does it happen, and what is one way to fix it?
Show answer
The component was destroyed, so its output is closed, and emitting on a closed output throws NG0953. Fix it by cancelling the timer when the component is destroyed, for example by calling clearTimeout in ngOnDestroy or in a callback registered with DestroyRef.onDestroy().