Update The Child Component And Parent Component Using @Input & @Output In Angular

This blog will show you how to pass the value from Parent Component to Child Component. And based on that value Child Component, performs operation and returns new value to Parent Component in Angular 4+

I have tried to make this concept as simple as possible. I hope anyone not clear with @Input and @Output decorators of Angular will understand after going through this blog.
 
Basic usage of @Input and @Output Decorators -
  • @Input - To pass data from Parent to Child Component
  • @Output - To pass data from Child to Parent Component
I have created 2 Components - TestParentComponent and TestChildComponent.

Task

I will add 2 numbers in the Parent component and pass that total value to the Child component. Later, using that value, I will perform the multiplication of text.

Input the value in Child Component and pass that updated value to the Parent Component.

Let's understand @Input to pass the data from Parent to Child Component by adding 2 numbers in Parent.

After generating, the component will be something like below.
  1. import {  
  2.     Component,  
  3.     OnInit  
  4. } from '@angular/core';  
  5. @Component({  
  6.     selector: 'app-test-parent',  
  7.     templateUrl: './test-parent.component.html',  
  8.     styleUrls: ['./test-parent.component.css']  
  9. })  
  10. export class TestParentComponent implements OnInit {}  
Now, first we will add two textboxes in test-parent.component.html file so that we can get the input values and perform the addition operation.
  1. <p> Parent Component </p>  
  2. <div class="form-group">  
  3.     <input type="text" class="form-control" placeholder="Number1" (input)="getTotalAmt()" [(ngModel)]="num1" name="num1" id="num1" />  
  4. </div>  
  5. <div class="form-group">  
  6.     <input class="form-control" placeholder="Number2" type="text" (input)="getTotalAmt()" [(ngModel)]="num2" name="num2" id="num2" />  
  7. </div>  
As you can see, I have added two text inputs which call the getTotalAmount() method on the input event. Now, I am updating the Parent class to perform the addition.
  1. export class TestParentComponent implements OnInit {  
  2.     num1: string;  
  3.     num2: string;  
  4.     Total: number;  
  5.     constructor() {}  
  6.     ngOnInit() {}  
  7.     ConvertToInt(val: string): number {  
  8.         return parseInt(val);  
  9.     }  
  10.     getTotalAmt() {  
  11.         this.Total = this.ConvertToInt(this.num1) + this.ConvertToInt(this.num2);  
  12.     }  
  13. }  
You will see the similar output shown below as per your inputs.
Update the Child Component and Parent Component using @Input & @Output in Angular 
Now, we will update the child component's HTML and add to the Parent. 
  1. <p> Child Component </p>  
  2. <div class="form-group">  
  3.     <input type="text" class="form-control" placeholder="Number3" />  
  4. </div>  
  5. <app-test-child></app-test-child> <!-- In test-parent.component.html add this code -->  
You will see the output as shown below.
 
 Update the Child Component and Parent Component using @Input & @Output in Angular
 
Now, I want to pass the Total to Child Component and perform the multiplication with Number 3 and update the Total in Parent.
 
Since I want to pass the Total from parent, I will add [Addition] as binding property in Parent component's HTML. And I will use the same as @Input variable for retrieving the data in Child.
  1. <app-test-child [Addition]="Total" ></app-test-child>  
Updating the Child Class.
  1. import {  
  2.     Component,  
  3.     OnInit,  
  4.     Input  
  5. } from '@angular/core';  
  6. export class TestChildComponent implements OnInit {  
  7.     @Input() Addition: number; //Note you need to import Input from @angular/core  
  8.     constructor() {}  
  9.     ngOnInit() {}  
  10. }  
To check if the addition variable is receiving the Total from Parent, you can simply create an interpolation and bind the Addition variable like this  {{Addition}} in Child Component's HTML file and see the output.
  1. <p>From Parent - {{Addition}}</p>  
Now, we will see the use of @Output to send the updated Total back to the Parent Component.
 
Let's update the Child Component class file. We will use Output and EventEmitter class for this. 
 
EventEmitter is used to emit the data to the Parent component.
  1. import {  
  2.     Component,  
  3.     OnInit,  
  4.     Input,  
  5.     EventEmitter,  
  6.     Output  
  7. } from '@angular/core';  
  8. @Component({  
  9.     selector: 'app-test-child',  
  10.     templateUrl: './test-child.component.html',  
  11.     styleUrls: ['./test-child.component.css']  
  12. })  
  13. export class TestChildComponent implements OnInit {  
  14.     @Input() Addition: number;  
  15.     @Output() valueUpdate = new EventEmitter();  
  16.     constructor() {}  
  17.     ngOnInit() {}  
  18.     updateValue(val) {  
  19.         this.valueUpdate.emit(val * this.Addition);  
  20.     }  
  21. }  
We declare valueUpdate as new EventEmitter and utilizie this to emit the data in updateValue() method. 
 
updateValue will get the input from Child Html file and perform the Multiplication. 
  1. <input type="text" class="form-control" [(ngModel)]="num3" (input)="updateValue(num3)" placeholder="Number3"/>  
Finally we need to update the parent html and class file to update these values as shown below.
  1. <app-test-child [Addition]="Total" (valueUpdate)="getUpdatedvalue($event)"></app-test-child>  
TestParentComponent Class,
  1. getUpdatedvalue($event) {  
  2.     console.log($event);  
  3.     this.Total = $event;  
  4. }  
That's it! You will see the below output,
 
Add 2 numbers in Parent as shown below,
Update the Child Component and Parent Component using @Input & @Output in Angular 
 
Now, add Number 3 to multiply with the total and the same will be updated in Parent.
 
 Update the Child Component and Parent Component using @Input & @Output in Angular