NaveenSingh
NaveenSingh's Blog

NaveenSingh's Blog

How to communicate between the components in Angular

NaveenSingh's photo
NaveenSingh

Published on Mar 21, 2021

3 min read

In Angular application everything is about component for displaying the data, So you must know how to use the component and communicate from one component to another component Based on your logic and condition.

By using Decorator you can communicate component easily

Two types of decorator

  1. Input() - (Parent Component to Child Component)
  2. Output() - (Child Component to Parent Component)

Parent Component to Child Component

In this technique let’s imagine an realtime example for easily to understand at anytime, Which I usually do, 
Parent component is about our Parents, Our mother or father and Child component is about their children, Son or daughter.

Now From the Parent Component sending data to ChildComponent,

Which means that Parent giving love, affection or food to the children, For Instance to understand

Now children what needs to do yes exactly they needs to accept those love or affection or food which is given by parents.

Let’s see in the Code

Screenshot 2021-03-21 at 11.10.28 PM.png

From the realtime example, The Childs needs to be under the control of parents, Same as like in the angular Inside the Parent Component should be ChildComponent in order to communicate.

Screenshot 2021-03-21 at 11.13.26 PM.png

And then next is about Child needs to accept which is given by parents like love or affection or food.

Screenshot 2021-03-21 at 11.15.28 PM.png

Under the child component you needs to put the message of parent component

Screenshot 2021-03-21 at 11.17.19 PM.png

Output :

Screenshot 2021-03-21 at 11.19.41 PM.png

ChildComponent to Parent Component

Let’s continued from previous story, In Previous parents has sended message to child as Hi Child!, Now Child will be replying Hi Momma!, By using output decorator and emitting the messages.

Screenshot 2021-03-21 at 11.39.14 PM.png

Output :

Screenshot 2021-03-21 at 11.41.58 PM.png

 
Share this