How To Pass Data Between The Component To Another Component In Angular

Introduction

 
In angular, we are able to pass different ways to share data between the components. Small components are always good to manage code in Angular. We have to define the relationship between the components. Please follow the below steps.
 
Here some ways to share data between components,
  • Parent to child component
  • Child to the parent component
  • Sharing data using ViewChild property

Parent to Child component

 
When you define @Input() in the child component it will receive value from the parent component. Before sharing data between components first we define our data model using interfaces.
 
When you declare a variable in the child component with the @Input() decorator, it allows that variable to be “received” from the parent component template.
 
Please follow the below to parent.component.ts file,
  1. import {  
  2.     Component  
  3. } from '@angular/core';  
  4. @Component({  
  5.     selector: parent - component '  
  6. })  
  7. exportclassParentComponent {  
  8.     messagelist = [{  
  9.         id: 1,  
  10.         msg: 'Test first notification'  
  11.     }, {  
  12.         id: 2,  
  13.         msg: 'Test first notification'  
  14.     }, ];  
  15.     constructor() {}  
parent.component.html
  1. <h1> Parent Component </h1>  
  2. <child-component[messagelist]="messagelist"></child-component>  
Here we have created messagelistlist and pass to send child.component.ts file to selector tag and receive the value from @Input().
 
Please follow the child.component.ts files codes below.
  1. import {  
  2.     Component,  
  3.     Input  
  4. } from '@angular/core';  
  5. @Component({  
  6.     selector: child - component '  
  7. })  
  8. exportclassChildComponent {  
  9.     @Input() messagelist: any[];  
  10.     constructor() {}  
  11. }  
  12. <divclass="messagelist"*ngFor="let m of messagelist ">  
  13.    {{ m.msg }}  
  14. </div>  

Child to parent component

 
If you want to pass data from the child component to the parent component use @Output() and EventEmitter.
 
To pass data from the child to the parent, you have to emit it from the child. The parent will be listening for the event will grab the data. Please follow the below codes.
 
child.component.ts
  1. import {  
  2.     Component,  
  3.     Output,  
  4.     EventEmitter  
  5. } from '@angular/core';  
  6. @Component({  
  7.     selector: 'child-component'  
  8. })  
  9. exportclassChildComponent {  
  10.     name: string = "";  
  11.     @Output() nameEmitter = newEventEmitter < string > ();  
  12.     PostData() {  
  13.         this.nameEmitter.emit(this.name);  
  14.     }  
  15.     constructor() {}  
  16. }  
child.component.html
  1. <div>  
  2.    <input[(ngModel)]="name"type="text"/>  
  3.    <button(click)="PostData()"> Pass Data </button>  
  4. </div>  
In our child component, we create a name emitter that sends the name anytime the user clicks the Post Data button. To receive the name in the parent, we have to create a function to grab that data and put it into a variable.
  1. import { Component } from'@angular/core';  
  2. @Component({  
  3.    selector:parent-component'  
  4. })  
  5. exportclassParentComponent {  
  6.    name = 'Welcome Test';  
  7.    constructor () { }  
  8.    receivename($event: string) {  
  9.    this.name = $event;  
  10.    }  
  11. }  
  12. <h2> {{ name }} </h2>  
  13. <child-component(nameEmitter)="receivename()"></child-component>  
In our parent component, (usernameEmitter) listens for the event with the message attached to it and when the message is emitted, it captures the data in the $event and sets this.username to the value of $event.
 

Using @ViewChild

 
Another simple way is to use the@ViewChild decorator. The child component file remains largely the same and the only changes we will make is to the parent.
  1. import { AfterViewInit, Component, ViewChild } from'@angular/core';  
  2. import { ChildComponent } from'./child/child.component';  
  3. @Component({  
  4.    selector:parent-component'  
  5. })  
  6.    exportclassParentComponentimplementsAfterViewInit {  
  7.       name = 'Welcome Test';  
  8.       @ViewChild(ChildComponent) child;  
  9.       constructor() { }  
  10.       ngAfterViewInit() {  
  11.          this.name= this.child.name;  
  12.          }  
  13.    }  
  14. <h2> {{ name }} </h2>  
  15. <child-component></child-component>  
We are able to pass the value using services, but once the page refreshes the value is not maintained in the service so, at that time again we call the API or some static data which must be assigned the to services. I will brieflyexplain in the next article how to pass the values using services.
 
I hope this article was mostly helpful.


Similar Articles