Sub topic 6
Declaring outputs with output()
This chapter assumes you know TypeScript classes and generics, Angular standalone components, and event binding such as (click)="..." in templates. It also assumes you have seen input() in the previous chapters. Inputs carry data down from a parent to a child. Outputs carry events up from a child to its parent.
What output() creates
output() is a function from @angular/core. It creates an output, which is a named event that a component can emit. You declare it as a class field. The type argument sets what kind of value the event carries.
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);
}
}The field changed holds an OutputEmitterRef<number>. Calling changed.emit(this.count) sends a number to any parent that listens. Each click increases count and tells the parent the new total.
The compiler checks the emitted value against the type argument. this.changed.emit('five') is a compile-time error here.
How an event travels
The parent subscribes to the output in its template. When the child calls emit(), Angular runs the parent's handler and passes the emitted value as $event. The next chapter covers the parent side in detail.
Events without a payload
Use output<void>() for an event that has nothing to report, such as a close button. Then call emit() with no argument.
import { Component, output } from '@angular/core';
@Component({
selector: 'app-dialog',
template: `<button (click)="closed.emit()">Close</button>`,
})
export class Dialog {
closed = output<void>();
}The template calls closed.emit() directly. This works, but for anything beyond one call, a method on the class is easier to read and test.
Sending richer values
An output carries a single value. To send several pieces of data, emit an object and give it a type.
import { Component, output } from '@angular/core';
interface Rating {
stars: number;
comment: string;
}
@Component({
selector: 'app-rating-form',
template: `<button (click)="submit()">Send</button>`,
})
export class RatingForm {
rated = output<Rating>();
submit() {
this.rated.emit({ stars: 4, comment: 'Clear and fast' });
}
}The parent's $event then has the type Rating, so the editor can autocomplete $event.stars.
Aliasing an output
You can expose an output under a different public name with the alias option. The class keeps its field name and parents use the alias.
valueChanged = output<string>({ alias: 'change' });Use aliases sparingly. They exist mostly to keep an older public name working while the class field gets a better name.
Rules and conventions
- Call
output()only in a field initializer or the constructor. It must run while Angular builds the component, because it needs that context to wire up the event. - An output is not a signal. You do not read it by calling it, and it has no current value.
emit()sends a value at one moment and stores nothing. - Name outputs for what happened, such as
changed,closed, orrated. Avoid anonprefix. The parent's handler is the place foron, as in(changed)="onChanged($event)". - Do not reuse the name of a native DOM event such as
clickorinput. A native event that bubbles up from inside your component can then trigger the parent's handler by accident.
Exercise
Exercise
A component declares saved = output<{ id: number }>();. Write the line that emits the event for the record with id 7, and say what type $event has in the parent's handler.
Show answer
this.saved.emit({ id: 7 }); In the parent, $event has the type { id: number }.
Exercise
Why is output() written as a class field initializer instead of being called inside a method such as increment()?
Show answer
output() must run in an injection context, which exists while Angular constructs the component. A field initializer or constructor provides that context. A later method call does not.