Welcome to the fifth part of Angular Basics. We will deep dive into services and dependency Injection in this part.
The service in Angular works as a central repository which can be accessed from any component. The starting point of the project can be taken from this zip file.
Our small app have three small components and we pass data around for logging and other things.
We will create a logging service first. So, create a file logging.service.ts inside the app folder. We will put the below content in it.
Now, we will use this logging service in our new-account.component.ts file. Here, we have to use it in providers and then in the constructor with type definition.
Now, we will also add the logging service in account.component.ts file
Now, when you check in the app both of them are working fine.
Now, we will look into another user case of service i.e. store and manage data. We will create a service accounts.service.ts which will contain our accounts detail, which is currently in app.component.ts file.
It will contain the accounts array and also, an addAccount and updateStatus function.
After that remove the accountAdded and statusChanged functions from app.component.html file.
Now, in app.component.ts file we will remove the array and the function and call our Account service
Now, in the new-account.component.ts file, we can get rid of the emitted name and status.
Instead of that we are adding accounts service in it.
Now, we will make the same changes in account.component.ts file, where we are adding the Account service and remove the old emitted service.
Now, we will not get any error, but our code is broken and in localhost we are not able to add any server.
We are actually having many different instances of the same service. So, we will keep only the one in the parent component of app.component.ts file.
We will first remove it from account.component.ts file.
And we will also remove it from new-account.component.ts file.
Now, we are again able to add a new server.
New serverNew server
Infact, we should remove the service from app.component.ts file also.
We will be adding it in the highest possible place, which is the app.module.ts file.
We will also learn to inject one service into another, so we will also be adding the logging service in app.module.ts file
Next, we will remove the logging service from account.component.ts file.
We are also removing the logging service from new-account.component.ts file.
Now, we will add the logging service in accounts.service.ts file. We also have to make it Injectable, as we adding adding a service to another service.
Now, we will learn to do cross-component communication using services. In account service we want to provide an event which we can emit from a component and catch in other component.
So, in accounts.service.ts file we will have an EventEmitter.
Now, we will first emit the event from account.component.ts file.
Now, we will catch the event in new-account.component.ts file by subscribing to it.
Now, we are able to communicate between components and the alert is shown on localhost.
This completes our post and you can find the final code in this github repo