Full answer
Angular gives several ways to move data. The best choice depends on how the two components are related.
- Parent to child: inputs. The parent binds a value with
[title]="x"and the child readstitle(). Data flows down only. - Child to parent: outputs. The child emits an event with
output(), and the parent listens with(chosen)="handler($event)". The child does not know who is listening, so it stays reusable. - Siblings or distant components: a shared service. A service provided in root (or at a route) holds a
signal, and any component that injects it can read or update it. This avoids passing data through many layers (prop drilling). - Parent calls child: a view query,
viewChild(ChildComponent), then call a public method. Use it for imperative actions such asfocus()orreset(), not for ordinary data. - Content projection: the parent passes markup in with
<ng-content>, for cases where the parent decides what the child shows. - Router: route parameters, query parameters, and resolver data carry data between pages.
A school example: a class teacher (parent) gives each student card (child) a name through an input. When a student presses "Absent", the card emits an event, and the teacher updates the register. A notice board (service) lets the library and the canteen see the same announcement without talking to each other.
Rules of thumb: prefer inputs and outputs for direct relations, because the data flow is easy to follow. Use a service when more than two levels are involved or when state outlives a component. Avoid reaching into another component's internals.
import { Component, Injectable, input, output, signal } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class NoticeBoard {
readonly message = signal('No notice today');
}
@Component({
selector: 'app-student-card',
template: `
<p>{{ name() }}</p>
<button (click)="absent.emit(name())">Absent</button>
`,
})
export class StudentCardComponent {
name = input.required<string>();
absent = output<string>();
}
@Component({
selector: 'app-notice-reader',
template: `<p>Notice: {{ board.message() }}</p>`,
})
export class NoticeReaderComponent {
constructor(protected readonly board: NoticeBoard) {}
}
@Component({
selector: 'app-register',
imports: [StudentCardComponent, NoticeReaderComponent],
template: `
@for (n of names; track n) {
<app-student-card [name]="n" (absent)="markAbsent($event)" />
}
<p>Absent: {{ absentNames().join(', ') || 'none' }}</p>
<app-notice-reader />
`,
})
export class RegisterComponent {
names = ['Asha', 'Imran', 'Meera'];
absentNames = signal<string[]>([]);
constructor(private readonly board: NoticeBoard) {}
markAbsent(name: string): void {
this.absentNames.update((list) => (list.includes(name) ? list : [...list, name]));
this.board.message.set(`${name} is absent today`);
}
}Angular 20.3, strict template check passes. Click "Absent" on any card and the summary and the notice both update.
Key points
- Down: `input()`. Up: `output()`. Across: a service with signals.
- `viewChild` is for imperative calls such as `focus()`, not for sharing state.
- Prefer the narrowest tool: direct relation means input and output.
- A service holding a signal avoids passing data through many levels.
Common mistakes
- Passing a value through five components with inputs instead of using a service.
- Mutating an object received through an input. The parent's copy changes too.
- Making a child depend on a specific parent by injecting it. The child is no longer reusable.
Follow-ups
- How would two components in different routes share an unsaved form?
- Why should a child not change an input object directly?
- When would you choose an event bus (Subject) over a signal in a service?
Tips
- Describe the relationship first (parent, sibling, distant), then name the tool.
- Say that outputs make a child reusable because it does not know its listener.
- Mention that a root service is shared for the whole app, so scope it to a route when it should reset.
Evaluation rubric
- Names inputs, outputs, services, and queries with correct direction of data.
- Gives a rule for choosing.
- Avoids anti-patterns such as mutation and tight coupling.