How to Communicate Between Components in Angular

Wojciech Szućko
Jul 18, 2017 · 3 min read

Multiple components make our application more reusable, but how do we communicate between them?

Source https://unsplash.com/photos/6bKpHAun4d8

Passing Data to a Component

We want to use the @Input() decorator from @angular/core to pass data to a component from the parent. Let’s review a simple example to see how this can be accomplished.

To begin, we need to create a new component ng g c name_of_your_component, I named my component first, then import the @Input() decorator and create a name variable:

Our first.component.html should look like this:

We can use our component in app.component.html, but what about the name variable? We can pass data to the component by using class="btn". Not clear? See the example below.

When you perform ng serve -o, you should see Hello, Asu! It’s an easy way to pass data to the component from the parent component.


Getting Data from the Component

There’s an@Output() decorator, just like the @Input(), and you can import it from @angular/core:

We changed our @Input() decorator to @Output() and initialized our name variable with Asu in the constructor.

In app.component.ts, we need to read the name variable like an object property but first let’s add angular id into app.component.html:

We can use the id #firstComponent in app.component.ts by using the@ViewChild decorator from @angular/core:

In line 10 you can see that we use @ViewChild('id'). In this case, the id is firstComponent. Further part is standard accessor name_of_variable: type. In ngOnInit, the function implemented by OnInit interface, we use the basic alert function to show name from the first component. You can try to do the same in constructor but in the console, you should see the error similar to Can't get name of undefined. That’s because on the constructor level our firstComponent isn’t live until our parent component is live.


The Ultimate Way to Communicate Between Components

Service! Yes, when use service to communicate between components, there’s no need to have parent-child or child-parent relations.

Generate the service ng g s services/Store --module=app. I added a --module flag to provide service in app.module.

I want to show you a bad example and for that, we need to do some changes in our components:

In line 14 we inject StoreService. It’s available from the store property. Let’s generate a second component called second:

And add it to app.component.html:

In this simple example, we changed the name in the second component, then refreshed it in the first component. That was a lot of work just to change the name, and the refresh is not what we want. Instead, we can use tools from the rxjs library called Subject and Observable. What’s the benefit in using these? name will refresh in thefirst component by itself, without our help.

Let’s cover you how to do that.

We can leave our second component how it is now, but we are going to change first component and StoreService.

In constructor, we subscribe to the function from StoreService called getName(); we can subscribe to this because it returns Observable. Subscribe means it will do everything inside subscribe whenever Observable changes.

We need to add async pipe inside our html. Pipe itself will handle stuff like displaying new values or unsubscribe to subscription after component will be destroy (it prevents memory leaks).

Observable will change whenever we will call thenext function on the Subject property, in our case, called name.

In my opinion, it’s the best way to communicate between components, but it depends on how many components we have in our Components Tree that we want to share data with.

Wojciech Szućko

Written by

http://wojciechszucko.com/

Better Programming

Advice for programmers.

More From Medium

More from Better Programming

More from Better Programming

More from Better Programming

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade