TExcelTAD
Home › Angular › Inputs & Outputs

Inputs & Outputs

Components become truly reusable when a parent can configure them and react to what happens inside them. Angular gives you two channels:

  • Inputs — data flows down from parent to child: <app-user-card [user]="selected()" />
  • Outputs — events flow up from child to parent: <app-user-card (follow)="onFollow($event)" />

Declaring inputs with input()

input() creates a read-only signal whose value is supplied by the parent:

angular · try it yourself
import { Component, input, computed } from '@angular/core';

@Component({
  selector: 'app-avatar',
  template: `
    <div class="avatar" [style.width.px]="size()" [style.height.px]="size()">{{ initials() }}</div>
  `,
  styles: `.avatar { border-radius: 50%; background: #dd0031; color: #fff; display: inline-flex;
            align-items: center; justify-content: center; font-weight: bold; margin-right: 8px; }`,
})
export class Avatar {
  name = input.required<string>();   // parent must provide it
  size = input(40);                   // optional, defaults to 40

  initials = computed(() =>
    this.name().split(' ').map((part) => part[0]).join('').toUpperCase(),
  );
}

@Component({
  selector: 'app-root',
  imports: [Avatar],
  template: `
    <app-avatar name="Bao Tran" />
    <app-avatar name="Elena Petrova" [size]="56" />
  `,
})
export class App {}
Form Meaning
input<T>() Optional input, undefined until set
input(default) Optional input with a default value
input.required<T>() Compile error if the parent forgets it
input(0, { transform: numberAttribute }) Converts the attribute string to a number
input(false, { alias: 'disabled' }) Exposes the input under another name

Static strings can be passed without brackets (name="Bao Tran"); anything else needs property binding ([size]="56").

Declaring outputs with output()

output() creates an event emitter. Call .emit(value) to notify the parent:

angular · try it yourself
import { Component, input, output } from '@angular/core';

export interface Product { id: number; name: string; price: number; }

@Component({
  selector: 'app-product-card',
  template: `
    <div class="card">
      <strong>{{ product().name }}</strong> — {{ product().price }} USD
      <button (click)="addToCart.emit(product())">Add</button>
    </div>
  `,
  styles: `.card { border: 1px solid #ddd; border-radius: 6px; padding: 8px 12px; margin-bottom: 6px; }`,
})
export class ProductCard {
  product = input.required<Product>();
  addToCart = output<Product>();
}

@Component({
  selector: 'app-root',
  imports: [ProductCard],
  template: `
    @for (p of products; track p.id) {
      <app-product-card [product]="p" (addToCart)="onAdd($event)" />
    }
    <p>Last added: {{ lastAdded }}</p>
  `,
})
export class App {
  products: Product[] = [
    { id: 1, name: 'Keyboard', price: 49 },
    { id: 2, name: 'Mouse', price: 19 },
  ];
  lastAdded = 'Mouse';

  onAdd(p: Product) {
    this.lastAdded = p.name;
    console.log('added', p.id, p.name);
  }
}
added 2 Mouse

In the parent, $event is the value passed to emit() — here the whole Product.

Data down, events up

The child never changes the parent's data directly. It asks by emitting an event and the parent decides what to do. This keeps the flow of data predictable. In plain TypeScript the pattern looks like this:

ts · try it yourself
interface Product { id: number; name: string; }

class ProductCard {
  constructor(
    public product: Product,                       // input
    private addToCart: (p: Product) => void,       // output
  ) {}

  clickAdd(): void {
    this.addToCart(this.product);
  }
}

class Shop {
  cart: string[] = [];
  cards = [
    new ProductCard({ id: 1, name: 'Keyboard' }, (p) => this.onAdd(p)),
    new ProductCard({ id: 2, name: 'Mouse' }, (p) => this.onAdd(p)),
  ];

  onAdd(p: Product): void {
    this.cart.push(p.name);
  }
}

const shop = new Shop();
shop.cards[1].clickAdd();
shop.cards[0].clickAdd();
shop.cards[1].clickAdd();
console.log(shop.cart);
[ 'Mouse', 'Keyboard', 'Mouse' ]

Reacting to input changes

Because inputs are signals, you derive values with computed() instead of overriding lifecycle hooks — the initials signal in the first example updates automatically whenever name changes.

Note: Older code declares inputs and outputs with decorators: @Input() name!: string; and @Output() addToCart = new EventEmitter<Product>();. They still work. The signal-based input() and output() are the modern equivalents, and the CLI offers a migration (ng generate @angular/core:signal-input-migration).

Test yourself

What does input.required<string>() guarantee?

  1. The value is never an empty string
  2. The parent template must bind this input, or compilation fails
  3. The input can be written by the child
  4. It is emitted as an event

How does a child component notify its parent that something happened?

  1. By setting the parent's fields directly
  2. By calling emit() on an output()
  3. By changing an input signal
  4. By calling ngOnChanges

In (addToCart)="onAdd($event)", what is $event?

  1. The DOM click event
  2. The value passed to addToCart.emit(...)
  3. The child component instance
  4. Always undefined