Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 74 ยท Components in Depth

Inputs and Outputs

Learn how Angular components talk to each other - input(), input.required, transform, alias, output(), the legacy @Input and EventEmitter, and one-way data flow.

Real applications are trees of components. A page contains a product list, the list contains product cards, a card contains a rating widget. For this to work, a parent needs a way to give data to a child, and a child needs a way to tell the parent something happened. Angular has exactly two tools for this: inputs (data flows down) and outputs (events flow up).

This lesson shows the modern signal-based functions, input() and output(), along with their options, a look at the older @Input() and EventEmitter you will meet in existing code, and the principle behind it all: one-way data flow. It builds on property binding and event binding.

Angular itself needs its compiler and CLI, which this site cannot run, so Angular code is read-only. The runnable <TryIt> examples are stand-ins in plain TypeScript and JavaScript that model the mechanics of inputs and outputs with hand-written classes.

Inputs with input()

An input is a property a parent can set from its template. You declare it in the child class with the input() function:

TypeScript
import { Component, input } from '@angular/core';

@Component({
  selector: 'app-user-card',
  template: `
    <h3>{{ name() }}</h3>
    <p>Role: {{ role() }}</p>
  `,
})
export class UserCardComponent {
  name = input.required<string>();
  role = input('guest');
}

The parent binds to it with property-binding syntax:

HTML
<app-user-card [name]="user().name" [role]="user().role" />
<app-user-card name="Ada" />

Four things to notice:

  • input() returns a read-only signal. You read it by calling it, name(), and the template re-renders when the parent changes it. The child cannot assign to it; only the parent controls the value.
  • input('guest') is an optional input with a default value. input<string>() with no argument is optional with the default undefined, so its type is string | undefined.
  • input.required<string>() is a required input: the compiler reports an error if a parent forgets to bind it, and its type has no undefined. Reading a required input before the parent has supplied a value (for example in the constructor) throws an error.
  • A plain attribute such as name="Ada" passes a constant string; use brackets when the value is an expression.

Because inputs are signals, you can build values from them with computed() and react to them with effect() (Signals: signal, computed and effect). That replaces most uses of the old ngOnChanges hook.

Transforming inputs

HTML attributes are always strings, but your component may want a number or a boolean. An input can declare a transform function that converts whatever the parent passes:

TypeScript
import { booleanAttribute, Component, input, numberAttribute } from '@angular/core';

@Component({
  selector: 'app-stepper',
  template: `...`,
})
export class StepperComponent {
  step = input(1, { transform: numberAttribute });
  disabled = input(false, { transform: booleanAttribute });
}

With these, <app-stepper step="5" disabled /> works: "5" becomes the number 5 and the bare disabled attribute becomes true. numberAttribute and booleanAttribute are built-in helpers; you can also write your own, such as a function that clamps a rating to the range 0 to 5. The transform should be a pure, simple function.

Aliases

TypeScript
size = input('medium', { alias: 'buttonSize' });

The parent writes [buttonSize]="...", while inside the class you still use size(). Use aliases sparingly: they exist mostly to keep a good public name when the internal name would clash with something else, and they make code harder to search.

Outputs with output()

An output lets the child announce an event. You declare it with output() and call .emit():

TypeScript
import { Component, output } from '@angular/core';

@Component({
  selector: 'app-rating',
  template: `
    @for (n of [1, 2, 3, 4, 5]; track n) {
      <button type="button" (click)="pick(n)">{{ n }}</button>
    }
  `,
})
export class RatingComponent {
  rated = output<number>();

  pick(n: number): void {
    this.rated.emit(n);
  }
}

The parent listens with ordinary event-binding syntax. The special $event holds the emitted value:

HTML
<app-rating (rated)="onRated($event)" />
TypeScript
onRated(score: number): void {
  this.score.set(score);
}

An output<void>() emits with no value, and an alias works as for inputs: output<number>({ alias: 'ratingChange' }). Name outputs after what happened (saved, selected, closed), not after the handler. Do not name an output like a native DOM event such as click, because the two will be confused.

The legacy decorators

Older code uses decorators instead of functions:

TypeScript
import { Component, EventEmitter, Input, Output } from '@angular/core';

@Component({ selector: 'app-rating', template: `...` })
export class RatingComponent {
  @Input() value = 0;
  @Input({ required: true }) label!: string;
  @Output() rated = new EventEmitter<number>();
}

They still work, and you will see them in many projects, but the function forms are the current recommendation: they are signals (so derived state and effects are simple), they type-check more strictly, and they need no decorators. EventEmitter is also an RxJS Subject under the hood, which output() deliberately is not. If you need to turn an Observable into an output, use outputFromObservable() (see Signals and RxJS Interop). The Angular CLI provides migrations to convert @Input and @Output automatically.

One-way data flow

flowchart TD
  P["Parent component<br/>owns the state"] -->|"[value] input (down)"| C["Child component<br/>shows the data"]
  C -->|"(rated) output (up)"| P

Data goes down through inputs; events come up through outputs, and the parent updates its state.

The rule of thumb: the component that owns the data changes it. A child that wants a change does not edit its input; it emits an event, and the parent decides what to do and passes the new value down. Benefits:

  • Predictable. When a value looks wrong you look in one place, the owner.
  • Reusable. The child works anywhere that supplies the same inputs and handles the same outputs.
  • Easy to test. Set the inputs, check the template; trigger an output, check the event.

Objects deserve a warning. An input holding an object or array gives the child a reference to the parent's data, and nothing stops the child from mutating it in place, which silently changes the parent's state. Treat input objects as read-only (a readonly TypeScript type helps) and emit events instead.

When a child really needs to read and write a value that the parent owns (a toggle, a stepper), model() combines an input and an output: see Two-Way Binding and model().

A runnable stand-in of one-way data flow

The first stand-in builds small versions of an input and an output. InputStandIn holds a value that only the parent writes (with an optional transform), and reading a required input before it is bound is an error. OutputStandIn is a list of listeners. A Rating child and a Review parent wire it together.

TypeScript Inputs down, outputs up

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

In this program the child's only way to change what it displays is to emit rated; the parent updates score and passes it back down. That round trip is the entire idea of input/output communication.

A runnable stand-in in the browser

The second stand-in is an HTML page with a parent that owns a list of products and a child "card" function that receives a product and a callback. The child can display data and report clicks, but it never modifies the list itself.

A parent owns the list, children report clicks

Click "Select" and "-10%" on different cards: the child only reports what the user did, and the parent changes the state and redraws. Angular does the redrawing for you, updating just the bindings whose values changed.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which declaration makes an input that a parent MUST bind?

    input.required() makes the compiler report an error if the parent does not bind it. input<string>() is optional with the value undefined.
  2. 2. A child component receives an input() signal. How does it change the value?

    An input signal is read-only for the child. Under one-way data flow the owner of the data changes it and passes the new value down.
  3. 3. What is the purpose of an input's transform option?

    transform runs on each value the parent supplies, so numberAttribute can turn "5" into 5 and booleanAttribute can turn a bare attribute into true.
  4. 4. In <app-rating (rated)="onRated($event)" />, what is $event?

    For a component output, $event is whatever the child emitted. DOM events only appear when you bind to a native event name.

Summary

  • input() creates a read-only signal input; input.required() forces the parent to bind it; { transform } converts incoming values and { alias } renames it.
  • output<T>() plus .emit(value) sends events up; the parent listens with (name)="handler($event)".
  • @Input() and @Output() with EventEmitter are the legacy equivalents and still work.
  • Data flows down through inputs and events flow up through outputs; the owner of the data changes it.
  • Do not mutate objects received through inputs; emit an event instead.
  • The runnable examples are stand-ins; the Angular snippets are read-only.

Next: Content Projection