Components in Angular are like small parts of a big machine. They need to talk to each other to work together and make the app useful.
Why component communication matters in Angular
Use @Input() to receive data from a parent component. Use @Output() with EventEmitter to send data to a parent component. Use a shared service with RxJS Subjects or Signals for communication between unrelated components.
@Input() and @Output() are decorators that help components share data up and down the component tree.
Services can be used to share data across components that do not have a direct parent-child relationship.
@Input() childMessage: string;
@Output() messageEvent = new EventEmitter<string>(); sendMessage() { this.messageEvent.emit('Hello from child'); }
import { Injectable } from '@angular/core'; import { Subject } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class SharedService { private messageSource = new Subject<string>(); currentMessage = this.messageSource.asObservable(); changeMessage(message: string) { this.messageSource.next(message); } }
This example shows a parent component sending a message to a child using @Input(). The child sends a message back using @Output() and an event. The parent listens and displays the child's message.
import { Component, EventEmitter, Input, Output } from '@angular/core'; @Component({ selector: 'app-child', template: ` <p>Child says: {{ childMessage }}</p> <button (click)="sendMessage()">Send Message to Parent</button> `, standalone: true }) export class ChildComponent { @Input() childMessage = ''; @Output() messageEvent = new EventEmitter<string>(); sendMessage() { this.messageEvent.emit('Hello Parent!'); } } @Component({ selector: 'app-parent', template: ` <h2>Parent Component</h2> <app-child [childMessage]="parentMessage" (messageEvent)="receiveMessage($event)"></app-child> <p>Message from child: {{ messageFromChild }}</p> `, imports: [ChildComponent], standalone: true }) export class ParentComponent { parentMessage = 'Hello Child!'; messageFromChild = ''; receiveMessage(message: string) { this.messageFromChild = message; } }
Always use @Input() and @Output() for parent-child communication to keep components clear and reusable.
For sibling or distant components, use shared services with RxJS or Angular signals to communicate.
Good communication between components helps keep your app organized and easier to maintain.
Components need to share data to work together smoothly.
Use @Input() and @Output() for parent-child data flow.
Use services for communication between unrelated components.
Practice
Solution
Step 1: Understand component roles
Components often need to share data to build a complete UI.Step 2: Recognize communication purpose
Sharing data helps components work together smoothly and stay in sync.Final Answer:
It allows components to share data and work together smoothly. -> Option BQuick Check:
Component communication = sharing data [OK]
- Thinking components work best completely isolated
- Confusing communication with performance optimization
- Believing Angular runs components without communication
Solution
Step 1: Identify Angular input binding syntax
Angular uses square brackets[]to bind data from parent to child.Step 2: Match syntax to data passing
[data]="parentData"correctly passesparentDatato child's@Input()property.Final Answer:
<child-comp [data]="parentData"></child-comp> -> Option DQuick Check:
Parent to child uses [property] binding [OK]
- Using parentheses () which are for event binding
- Using template reference # incorrectly
- Using structural directive * wrongly
Child component TS:
@Output() notify = new EventEmitter<string>();
sendNotification() { this.notify.emit('Hello'); }
Parent component template:
<child-comp (notify)="onNotify($event)"></child-comp>
Parent component TS:
onNotify(message: string) { console.log(message); }
Solution
Step 1: Understand event emission
The child emits 'Hello' vianotify.emit('Hello').Step 2: Check parent event binding
The parent listens with(notify)="onNotify($event)", soonNotifyreceives 'Hello'.Final Answer:
Hello -> Option AQuick Check:
Child event emits 'Hello' caught by parent [OK]
- Confusing @Output with @Input
- Not passing $event in parent handler
- Expecting no output without calling emit
Parent template:
<child-comp data="parentData"></child-comp>
Child component TS:
@Input() data: string;
Solution
Step 1: Identify correct binding syntax
Passing data from parent to child requires property binding with square brackets.Step 2: Analyze given code
Usingdata="parentData"passes a string literal, not the variable value.Final Answer:
The parent should use square brackets: [data]="parentData". -> Option CQuick Check:
Property binding needs [] around attribute [OK]
- Using string literals instead of variable binding
- Confusing @Input and @Output decorators
- Using event binding () for data input
Solution
Step 1: Identify component relationship
Unrelated components do not have parent-child relation, so @Input/@Output won't work.Step 2: Choose communication method
A shared service with an observable allows broadcasting data to any component.Final Answer:
Use a shared service with an observable to hold and broadcast the login status. -> Option AQuick Check:
Unrelated components communicate via shared service [OK]
- Trying to use @Input/@Output between unrelated components
- Using local variables that don't share data
- Misusing template reference variables for communication
Ready to prove it?
Pass the test and this pattern moves unknown
