Parent-Child Components
Angular applications are built using reusable components. A parent component can contain one or more child components and can communicate with them using component inputs and outputs.
What are Parent-Child Components?
A parent component is a component that contains another component inside its template.
The component placed inside the parent template is called the child component.
Simple Example
Suppose StudentComponent is the parent and StudentDetailsComponent is the child. The parent can send student information to the child.
Parent-Child Component Structure
Parent-Child Communication
Angular commonly uses two directions of communication:
Parent → Child
The parent sends data to the child using an input property.
@Input
Child → Parent
The child sends events or data back to the parent using an output property.
@Output
Parent to Child Communication
The parent component can pass data to a child component using
@Input.
Step 1: Create Parent Component
import { Component } from '@angular/core';
@Component({
selector: 'app-student',
standalone: true,
templateUrl: './student.component.html'
})
export class StudentComponent {
studentName = 'Sam';
}
Step 2: Use Child Component in Parent
The child component selector is placed inside the parent template.
<h2>Student Information</h2>
<app-student-details
[name]="studentName">
</app-student-details>
Here the parent sends the value of studentName to the
child's name input.
Step 3: Create Child Component
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-student-details',
standalone: true,
template: `
<h3>Student Details</h3>
<p>Student Name: {{ name }}</p>
`
})
export class StudentDetailsComponent {
@Input() name = '';
}
Student Details
Student Name: Sam
Using Input
The input property receives a value from the parent component.
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-student-details',
standalone: true,
template: `
<p>Student: {{ name }}</p>
`
})
export class StudentDetailsComponent {
@Input() name = '';
}
Child to Parent Communication
A child component can communicate with its parent using
@Output and an event emitter.
Child to Parent Example
import { Component, Output, EventEmitter }
from '@angular/core';
@Component({
selector: 'app-student-details',
standalone: true,
template: `
<button (click)="sendMessage()">
Send Message
</button>
`
})
export class StudentDetailsComponent {
@Output() message = new EventEmitter<string>();
sendMessage() {
this.message.emit('Hello Parent');
}
}
The parent listens to the event from the child.
<app-student-details
(message)="receiveMessage($event)">
</app-student-details>
Complete Parent-Child Example
import { Component } from '@angular/core';
import { StudentDetailsComponent }
from './student-details.component';
@Component({
selector: 'app-student',
standalone: true,
imports: [StudentDetailsComponent],
template: `
<h2>Student Profile</h2>
<app-student-details
[name]="studentName">
</app-student-details>
`
})
export class StudentComponent {
studentName = 'Pradnya';
}
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-student-details',
standalone: true,
template: `
<div class="student-details">
<h3>Student Details</h3>
<p>Name: {{ name }}</p>
</div>
`
})
export class StudentDetailsComponent {
@Input() name = '';
}
Student Profile
Student Details
Name: Pradnya
Parent-Child Communication Summary
| Direction | Angular Feature | Purpose |
|---|---|---|
| Parent → Child | @Input |
Pass data to child. |
| Child → Parent | @Output |
Send events to parent. |
| Child → Parent | EventEmitter |
Emit custom events. |
| Parent → Child | Property Binding | Bind parent values to child inputs. |
Benefits of Parent-Child Components
Reusability
Child components can be reused in different parent components.
Communication
Components can exchange data and events.
Organization
Large interfaces can be divided into smaller components.
Maintenance
Smaller components are easier to test and maintain.
Best Practices
- Keep parent components responsible for overall page-level data.
- Use inputs to pass data from parent to child.
- Use outputs to communicate child events to the parent.
- Keep child components focused on a specific responsibility.
- Avoid unnecessary communication between deeply nested components.
- Use reusable child components for repeated UI functionality.
Summary
Parent-child components allow Angular applications to divide a large interface into smaller and reusable components.
@Input is commonly used to pass data from a parent to a child, while @Output with EventEmitter is used for child-to-parent events.
Understanding component communication is essential for building organized and reusable Angular applications.