What is a service in Angular? Why are services needed in Angular?
A service in Angular is a class with business logic that is not tied to the view and can be used across different components.
Why they are needed:
- To move repetitive code out of components (requests, calculations, data processing).
- To organize shared data, for example storing state, a cache, or the current user.
- To communicate with the server (HTTP requests, an API).
- To separate responsibilities: the component handles the UI, the service handles the logic.
Services are registered in the DI system, and Angular creates and injects instances wherever they are needed.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.