How to communicate and share data from parent component to child component (@Input decorators) and from child component to parent component (@Output decorators).
Introduction
- Angular provides data sharing decorators to communicate between the parent component to the child component using the @Input decorator and from the child component to the parent component using the @Output decorator,
- Here @Input() property is writable and an @Output() property is observable.
- So let's understand more by using the examples below.
@Input decorator
- The @Input decorator allows a component to read data from a parent component to a child component by defining a variable in the child component with the @Input decorator.
- The @Input decorator is specified in the '@angular/core' module, and we can use it by importing from there.
- Syntax to define Input decorator is :
@Input() variableName :<dataType> - So here I defined
dataFromParentvariable using as @Input decorator to read data from the parent component - Now in the parent component, I use a child component selector here [dataFromParent]="passDataFromParent" to pass data to the child component
- The child component read that in dataFromParent property and uses it in the template to display data.
parent.component.html
<div class="wrapper">
<app-child
[dataFromParent]="passDataFromParent"
></app-child>
</div>
child.component.ts
import { Component, Input, OnInit } from '@angular/core';
import { ChildData } from '../../SAMPLE_DATA';
@Component({
selector: 'app-child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.scss'],
})
export class ChildComponent implements OnInit {
@Input() dataFromParent!: ChildData[];
}
child.component.html
<h2>Child Component</h2>
<div class="child-wrapper">
<ng-container *ngFor="let item of dataFromParent">
<mat-card align="center">
<mat-card-content> {{ item.name }} </mat-card-content>
<button
mat-raised-button
color="primary">
Select
</button>
</mat-card>
</ng-container>
</div>
We show passing data from the parent component to the child component.
The output of @Input decorator example
As you can see here, I clicked any food products and passed that data from the parent component to the child component and the child component displayed those data in card format.
@Output decorator
- The @Output decorator allows a component to pass(emit) data from a child component to a parent component by defining an EventEmmiter in the child component with the @Output decorator.
- The @Output decorator is specified in the '@angular/core' module, and we can use it by importing from there.
- Syntax to define Output decorator is :
@Output() newItemEvent = new EventEmitter<dataType>(); - So first In the child component, I defined @Output decorator as
@Output() onClick = new EventEmitter(); - Now here in the child component template using the select button I trigger passDataToParent(item) method.
- And using onClick EventEmitter I emit that output event and pass data to the parent component.




Join the conversation! Your thoughts help the community grow.