Input and Output — Angular

Inputs and outputs connect a parent component (one that uses another component in its template) with a child component (the one it uses). @Input() name = ''…

In the components lesson, all three menu cards say the same thing: Special Egg Pancake, $10. That's because the name and price are fixed in the MenuItem class. The cards can't tell App when someone orders, either.

Two components talk to each other through inputs and outputs. A component that uses another component in its template is called the parent component. The component it uses is called the child component. Here the parent is App, and the child is the menu card.

Picture blank menu cards at Jali's Pancake Stall. Jali plays the part of App. He writes a name and a price on each card: that's an input. Each card has a little bell labelled Order. When the bell on one card rings, Jali hears it and puts the pancake from that card in the basket: that's an output.

@Input(): App fills the card

The menu card now has two inputs, name and price:

// src/app/menu-item/menu-item.ts
import { Component, Input } from '@angular/core'

@Component({
  selector: 'app-menu-item',
  standalone: true,
  template: `
    <div class="card">
      <h3>{{ name }}</h3>
      <p>{{ '$' + price }}</p>
    </div>
  `,
})
export class MenuItem {
  @Input() name = ''
  @Input() price = 0
}

In App's template, @for creates one card for each menu item and fills both of its inputs:

<!-- App's template, with imports: [MenuItem] -->
@for (m of menu; track m.id) {
  <app-menu-item [name]="m.name" [price]="m.price" />
}

The menu data in App's class:

menu = [
  { id: 1, name: 'Chocolate Cheese Pancake', price: 8 },
  { id: 2, name: 'Special Egg Pancake', price: 10 },
  { id: 3, name: 'Pandan Pancake', price: 7 },
]

The exercise uses the same shape:

@Component({
  selector: 'app-greeting',
  standalone: true,
  template: `<h2>Hello, {{ name }}!</h2>`,
})
export class GreetingComponent {
  @Input() name = ''
}

In the exercise editor this component runs alone, with no parent. Nobody sends a name, and the starting value of name is empty text, so it shows Hello, !. That's expected.

Square brackets read code, not text

What's inside the quotes of [name]="..." is read as TypeScript code, just like what's inside {{ }}. So:

Written in App's template What the card receives
[name]="m.name" the value of the property m.name
[name]="'Special Egg Pancake'" the text Special Egg Pancake
name="Special Egg Pancake" the same text, without square brackets
[message]="Hello" App's property named Hello, which doesn't exist, so nothing
[name]="Special Egg Pancake" nothing: the template fails to compile

In the last row, Special Egg Pancake without quotes is three words, not valid code. So Angular 18 refuses App's whole template: Parser Error: Unexpected token 'Egg' at column 9 in [Special Egg Pancake]. No card shows, not even App's heading. To send text, put single quotes inside the double quotes, or remove the square brackets.

@Output(): the card tells App

The menu card gets an output named order, and a method orderThis() that the Order button calls:

// in MenuItem: add Output and EventEmitter to the import
@Output() order = new EventEmitter<string>()

orderThis() {
  this.order.emit(this.name)
}
// the card's template: <button (click)="orderThis()">Order</button>

In App's template, the parent listens for that output and shows what's in the cart, separated by commas:

<!-- App's template -->
<app-menu-item [name]="m.name" [price]="m.price" (order)="addToCart($event)" />
<p>Cart: {{ cart.join(', ') }}</p>

In App's class:

cart: string[] = []

addToCart(name: string) {
  this.cart = [...this.cart, name]
}

What the cart holds after two orders:

Event $event Shows
The page first shows Cart:
Order on the Pandan Pancake card 'Pandan Pancake' Cart: Pandan Pancake
Order on the Special Egg Pancake card 'Special Egg Pancake' Cart: Pandan Pancake, Special Egg Pancake

Data flows one way

Data through an input only flows down, from parent to child. If the card changes its own this.name, only that card changes: App's menu stays the same. As soon as App changes that name in menu, Angular overwrites the card's copy with the new value. So a child that wants to change its parent's data doesn't change it itself. The child emits an output, and the parent makes the change, like addToCart above.

💡 @Input options: required and transform

You can write options inside the brackets of @Input(). Two common ones:

@Input({ required: true }) name!: string
@Input({ transform: booleanAttribute }) favorite = false

💡 The signal form: input() and output()

Inputs and outputs can also be written with functions that create a signal:

name = input('')
price = input.required<number>()
order = output<string>()

⚠️ Blank cards and bells nobody hears

TL;DR: Inputs and outputs connect a parent component (one that uses another component in its template) with a child component (the one it uses). @Input() name = '' makes a property the parent fills through [name]="m.name". What's inside square brackets is read as code, so text needs single quotes ([message]="'Hello'" sends Hello), or no square brackets at all. @Output() order = new EventEmitter<string>() plus this.order.emit(this.name) sends an event up to the parent. The parent listens with (order)="addToCart($event)", where $event is the emitted (sent) value. Data flows down through inputs, events flow up through outputs. input() and output() are the signal form, for Angular CLI projects.

⚡ @Input: the parent fills the child

Write:

@Component({
  selector: 'app-greeting',
  template: `<h2>Hello, {{ name }}!</h2>`,
})
export class GreetingComponent {
  @Input() name = ''
}

Meaning: @Input() marks name as an input: a property that may be filled from outside, by the parent component (the component that uses <app-greeting> in its template). The parent fills it with square brackets, for example <app-greeting [name]="m.name" /> (m here is one item of the parent's data). When this component runs alone with no parent, nobody sends a name, so it shows Hello, !. That's expected.

The exercise checks for @Input() followed directly by name, and for {{ name }} inside <h2>.

⚡ Square brackets hold code

Write:

<app-menu-item [name]="m.name" [price]="m.price" />
Guess first: in this line, the parent fills the inputs of a menu card. If the parent writes [name]="Special Egg Pancake", what does the card receive?

Nothing. Three words without quotes aren't valid code, so the parent's whole template fails to compile. Write [name]="'Special Egg Pancake'", or remove the square brackets.

Meaning: What's inside [name]="..." is read as TypeScript code, just like {{ }}.

⚡ @Output: the child tells the parent

Write:

@Output() order = new EventEmitter<string>()

orderThis() {
  this.order.emit(this.name)
}

Meaning: @Output() marks order as an output: an event the parent can listen for. EventEmitter<string> is what sends it, and each event carries one piece of text. orderThis(), the method the Order button calls, runs this.order.emit(this.name): it sends that event with the card's pancake name inside. The parent listens with (order)="addToCart($event)", and $event there holds that pancake name. Data flows down through inputs, events flow up through outputs.

What you will learn