Inputs and Outputs
Components talk to each other. A parent sends data down to a child with an input, and the child sends events up with an output.
Real apps are trees of components that must share data. A product list (parent) knows the products; each product card (child) shows one. The parent passes each product down as an input. When the user clicks "Add to cart" inside the card, the card can't change the cart itself - it raises an output event, and the parent decides what to do.
This "data down, events up" pattern keeps child components simple and reusable: a product card doesn't need to know about carts or servers, so you can use it on the home page, the search page and the wishlist.
Example
import { Component, input, output } from '@angular/core';
@Component({
selector: 'app-product-card',
template: `
<h3>{{ name() }} - Rs {{ price() }}</h3>
<button (click)="addToCart.emit(name())">Add to cart</button>
`,
})
export class ProductCard {
name = input(''); // data coming in
price = input(0);
addToCart = output<string>(); // event going out
}The parent sets inputs with [ ] and listens to outputs with ( ):
Example
<app-product-card
[name]="'Headphones'"
[price]="1299"
(addToCart)="onAdd($event)" />Inputs made with
input() are signals, so read them with brackets: name().Key points
input()receives data from the parent.output()sends events up to the parent.- Parent:
[input]="value"and(output)="handler($event)".
📝 Test yourself
1. Which function receives data from a parent component?
2. How does a child send an event to its parent?
3. Fill in the method that sends an output event:
addToCart.(name());
