A ‘Service’ in Angular is a class intended to perform a specific task. Generally, we create services in Angular based on our objective. Each and every objective will have its own service.
Let’s take an example of a simple mathematical operation: Sum of two values should be logged in the output.
In order to perform this, we need to write the logic to do the arithmetic operation and based on the logic, we can see the result in the output.
The location where are we writing this logic i.e. in which file makes a considerable difference as explained below.
In general, we write the code inside the ‘component’ and it works fine. But, the actual purpose of a component is to aid in providing the methods and properties to data binding in the process of mediating between the ‘application logic’ and ‘view’.
So, to make the component’s job easy, we divide the tasks and create separate ‘services’ in Angular for individual tasks. In addition to this, we can even use these services in multiple components when required, by which we can eliminate the code duplication.
How do we use services in Angular?
To share the data across multiple components.
To implement application logic.
For external interactions like connecting to a database.
Now, How to use a service inside a component?
As we are creating multiple services in Angular based on our objectives, we should integrate such services with the component. That means we make the service available for the component to interact with it.
To see the example of service creation and it's integration check my article Services in Angular