Sharing Data Between Angular Components Via @Input() And @Output()

In this article we will learn how to pass data from parent component to the respective child component and vice versa in Angular 8.
 

Introduction

 
Pass data from parent to child component using @Input() decorator, which allows data to pass through templates and child to parent component using @Output() decorator with the help of Event Emitter.
 
Prerequisites
  • Basic knowledge of Angular
  • Visual Studio Code must be installed
  • Angular CLI must be installed
  • Node JS must be installed
Step 1
 
Create a new Angular project using the following NPM command:
  1. ng new componentSharing
Step 2
 
Create a Parent Component using the following NPM command:
  1. ng g c parent
Step 3
 
Next step is to create a Child Component using the following NPM command:
  1. ng g c child 
Step 4
 
Give parent component selector in app.component.html:
  1. <app-parent></app-parent>   
Step 5
 
In this step we are creating a textbox and a submit button so when we click on submit data from text box would get transfer to child component.
 
Open parent component.html file and paste  the below code inside.
 
parent.component.html 
  1. <div class="col-12 col-md-12">    
  2.   <div class="card">    
  3.     <div class="card-header">    
  4.       Parent Component    
  5.         
  6.     </div>    
  7.     <div class="card-body">    
  8.       <form>    
  9.       <div class="order-tracking">    
  10.        <input name="yourname" type="text" [(ngModel)]="name" placeholder="Enter your name"/>    
  11.        <button type="button" (click)="submit()" class="btn-btn primary">Submit</button>    
  12.     
  13.        <h3>{{this.message}}</h3>    
  14.       </div>    
  15.     </form>    
  16.     </div>    
  17.   </div>    
  18. </div>    
  19. <app-child [Name]="sendToChild" (getResponse)="getResponse($event)"></app-child>     
 
Here in the above we are sending data in child component using app-child selector .
 
Step 6
 
Open parent component.ts file and paste the below code inside.
 
parent.component.ts
  1. import { Component } from '@angular/core';  
  2.   
  3. @Component({  
  4.   selector: 'app-parent',  
  5.   templateUrl: './parent.component.html',  
  6.   styleUrls: ['./parent.component.css']  
  7. })  
  8. export class ParentComponent {  
  9.   name: string;  
  10.   message: any;  
  11.   sendToChild: string;  
  12.   
  13.   getResponse($event) {  
  14.     this.message = $event;  
  15.   }  
  16.   submit() {  
  17.     this.sendToChild = this.name;  
  18.   }  
  19. }  
Step 7
 
Open child component.html file and paste the below code inside.
 
child.component.html
  1. <div class="col-12 col-md-12">    
  2.   <div class="card">    
  3.     <div class="card-header">    
  4.      Child Component    
  5.         
  6.     </div>    
  7.     <div class="card-body">    
  8.       <div class="order-tracking">    
  9.         {{this.Name}}    
  10.       </div>    
  11.       <button type="button" (click)="onClick()">Ok</button>    
  12.     </div>    
  13.   </div>    
  14. </div>     
Step 8
 
Open child component.tsfile and paste the below code inside.
 
child.component.ts
  1. import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';  
  2.   
  3. @Component({  
  4.   selector: 'app-child',  
  5.   templateUrl: './child.component.html',  
  6.   styleUrls: ['./child.component.css']  
  7. })  
  8. export class ChildComponent implements OnInit {  
  9.   @Input() Name;  
  10.   @Output() getResponse = new EventEmitter;  
  11.   constructor() { }  
  12.   
  13.   ngOnInit() {  
  14.   }  
  15.   onClick() {  
  16.     this.getResponse.emit('Message from child');  
  17.   }  
  18. }  

Output

Conclusion

 
In this article we have seen how to transfer data fron parent component to child component and vice versa using @Input() and @Output() decorator .
 
I hope you have enjoyed this article as much as I have enjoyed writing and coding the examples.
 
Please let me know in the comments how I could improve it and any suggestions from your end.


Similar Articles