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.
- An input carries data from the parent down to the child: App tells a menu card its name and price.
- An output carries an event from the child up to the parent: the card tells App "someone ordered me".
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 },
]
@Input()is a decorator that marks a property that may be filled from outside, by its parent. Without it, the property only lives inside its own component.[name]="m.name"in App's template is property binding to the card: the value ofm.namegoes to that card'snameproperty. The result is three cards from one class: Chocolate Cheese Pancake $8, Special Egg Pancake $10, Pandan Pancake $7.name = ''is the starting value. It's used for as long as the parent sends nothing.- If you forget to write
@Input(), Angular refuses the binding. The card is empty, and the console writesNG0303: Can't bind to 'name' since it isn't a known property of 'app-menu-item'.
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 |
@Output()marks an event the parent can listen for.EventEmitter<string>is what sends that event, and<string>means each event carries one piece of text.this.order.emit(this.name)sends (emits) the event to the parent, with the name of the pancake on that card inside it. At Jali's stall, this is the moment the bell rings.- In App's template,
(order)works like(click).$eventhere holds the emitted value, the pancake's name, not a DOM event object. - The event name in App must match the output property's name exactly.
(ordered)catches nothing, and no error appears.
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
required: truemakes the input required: an Angular CLI project's build fails when the parent forgets to send[name]. The exercise editor doesn't check this: the card still shows, with an empty name. The!means the parent always sendsname, so it needs no starting value.transform: booleanAttribute(imported from@angular/core) turns an attribute written without a value into a boolean. So<app-menu-item favorite />givesfavoritethe valuetrue. Without the transform, its value is the empty text''.- An attribute without square brackets always sends text:
price="10"gives the text'10', not a number. To send a number, use[price]="10", ortransform: numberAttribute.
💡 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>()
- Angular's documentation now recommends this form for new code. You read the value by calling it like a function:
{{ name() }}. If you forget the round brackets,{{ name }}shows the signal function as text, like[Input Signal: ...]. - The value of an
input()can't be changed from inside the card: there is nothis.name.set(...). - The exercise editor and the Playground run Angular 22 without the Angular CLI. There
input(),output()andmodel()(covered in the next lesson) aren't recognised as inputs or outputs. The console writesNG0303: Can't bind to 'name' since it isn't a known property of 'app-menu-item'. That's why the exercise and the Playground use@Input()and@Output().
⚠️ Blank cards and bells nobody hears
- Text without quotes in square brackets.
[name]="Special Egg Pancake"breaks the whole template. Write[name]="'Special Egg Pancake'"orname="Special Egg Pancake". - Forgetting
@Input(). The binding is refused with NG0303, and the card is empty. - An event name that differs from the output's name.
(ordered)for the outputordercatches nothing, with no error. - Another form in the exercise. The exercise checks that the code contains
@Input()followed directly byname, and{{ name }}inside<h2>.@Input({ required: true }) nameorname = input('')don't pass that check.
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.