Sharing data between the Angular components

Welcome beginners, yes this is more or less for the developers who just started their journey as Angular 2+ developer or want to learn Angular 2+. Most of the new Angular Developers face problems in sharing the data between the components. Also if you are applying as fresher or junior level then chances are high, that it is one of the question you may be asked during your interview. I have tried to make it as simple as it can be.

We will cover five different methods for sharing the data between the Angular components:

  1. Using Input decorator
  2. Using Output decorator
  3. Using services
  4. Using ViewChild
  5. Using route parameters

On most if the places you will see commonly the first four methods to pass data between the components. But believe me sharing data using DOM is also important some scenarios(we will discuss in later section).

OK, so let’s get started.

Now there are scenarios where you have to apply different approach. According to those scenarios, lets start discussing from the top:

Using Input decorator

This is the most common and very useful way to share data between the components. Suppose when you have parent-child relationship between the two components and you want to pass the data from parent to child, then is where Input decorator comes into the picture.

Child component code

In the above code I have declare the childName property using the Input decorator. You can use this as a global variable for this ChildComponent class.

Parent component code

Now in the parent component I have used the ChildComponent selector to bind the child component in the parent and using [] in the app-child tag I can pass the the data to the child component.

So simple isn’t it.

Using the Output decorator

Now you are wondering I want to pass the data to the parent from the child, so how can I do so.

Just wait, I will show you how simple is that. Let’s modify the child component first for our need.

In the above code I have declared the event emitter using Output decorator and in the constructor I am emitting an event which we can listen in our parent component. See below,

In the above code whenever we emit the event from the child component then then onGameChange method will be called. In this way you can easily pass the data from child to parent.

Using services

Now suppose you have two child components as siblings and you want to pass data from one sibling to another. Then the combination of above two approach can be used like emitting the data from one child component to parent and passing that data to another child. That is good when you do not have many nested components, otherwise the number of event emitters may increase.

This can be solved using angular services and RxJS Observables and Subject. I will show you to code a message bus in Angular project which can deliver data between any component.

Message service

First Component

Second Component

So using the above approach you can observe the to multiple places or you can send the flag that this data is for which component.

Using ViewChild

Using this method you can directly access the child component public properties. This method is also useful when you want access the child component properties.

ViewChild is query selector which selects the element from the template using selector passed in the query. See below:

Child component code

Parent component code

So in this way you can query the view and access the child properties. But always remember to use the AfterViewInit lifecycle hook with ViewChild decorator as query is made after the view is initialised.

Using route parameters

This is again the common approach to edit or access the data based on the IDs. Suppose I have a course which have 10 chapters. Now I have a list of all the chapters on the parent component and I want to edit one chapter.

So using this approach when you navigate to different router using the chapter ID, you can catch that ID from the child route using the ActivatedRoute Angular core API.


So that’s all about sharing the data between the Angular components. Always master these tools to make Modular Angular Applications.

“Always share good thoughts, It will come back to you..!”