@Input() and @Output() are the primary mechanism for communication between parent and child components in Angular.
// child.component.ts
import { Component, Input } from '@angular/core';
@Component({ selector: 'app-product-card', standalone: true, /* ... */ })
export class ProductCardComponent {
@Input({ required: true }) product!: Product;
@Input() currency: string = 'USD';
// Input with transform (Angular 16+)
@Input({ transform: (v: string) => v.trim() }) title: string = '';
}
<!-- parent template -->
<app-product-card [product]="selectedProduct" currency="EUR" />
import { Component, Input, Output, EventEmitter } from '@angular/core';
@Component({ selector: 'app-product-card', standalone: true, /* ... */ })
export class ProductCardComponent {
@Input({ required: true }) product!: Product;
@Output() addToCart = new EventEmitter<Product>();
@Output() toggleWishlist = new EventEmitter<number>(); // emits product id
onAddToCart(): void {
this.addToCart.emit(this.product);
}
}
<!-- parent template -->
<app-product-card
[product]="product"
(addToCart)="handleAddToCart($event)"
(toggleWishlist)="handleWishlist($event)"
/>
@Input() value: string = '';
@Output() valueChange = new EventEmitter<string>();
// Parent can use [(value)]="myVar"