Finding your third day with Angular

Hey, guys, I am back again with some weird discussion on angular and following up with my previous discussion link.
Today I am discussing some of the cool topics that you first encounter while writing your first angular application ie. Communication between two component which is very cliche topic among angular guys.

I am here not explaining the number of the way we can achieve this because this link contains.
Instead, I am Explaining here about I use to do usually.

I believe making discussion open like and guide you all the ways will mislead/distract you instead(pardon if you does not think the sameπŸ‘). I will tell you the proper and decent way of doing this.

Since Explaining code is not my concern but the Logic says

If you need to make something sync with two component you need to communicate it using services

"Service" is not an Extraordinary jargon but a modular way of approaching this very regular thing.
So How we write our first service

ng g s ServiceName    thats it.

In this file, you see there is written inject in the root. You should know what it is. It says an angular application that whoever inside the application will use this service the property will same for all. so the service should respond to the application root.

I told you the story in a very funky way but for now, it is enough.


The Line I said use this service came up with a term called DEPENDENCY INJECTION.
I am really sorry for this jargon but you know what it is a common concept but hard to digest.

So how it works like how this gets synced up.


I have comp A

Another comp B


A wants to update B

Both components are independent or in Parent-child relationship doesn't matter.

A use service and update the data in service while B uses the service data and update it.πŸ˜€πŸ˜πŸ˜‚.


I am sorry I should have told you how I promise I won't write the code. So down the line, I can make you understand.

You just need to make the property using subject Behaviour (using RXJS lib) inside service.

this subscribable variable can be set(updated ) and fetched with the latest result in anywhere whoever have subscribed to it.


I think enough is enough to show me some code. Sorry I won't please search over I have already told you so many jargons.


Find my profile over linkLink :https://www.linkedin.com/in/kripa-shankar-jha/



Comments

Popular Posts