What is the difference between a component and a service?
Model answer
A component controls part of the screen: it has a template (HTML), styles and a class with data and event handlers. A service is a plain class for logic that is not tied to one screen, such as calling an API, keeping shared state or logging.
Keeping this logic in services means several components can share it and it is easy to test. Components get services through dependency injection.
Tips
- Give a real example, such as a UserService used by two components.
Learn this topic
Angular Tutorial: Beginner to Advanced .
Sign in to add your own answer.
More questions
- What is Angular, and how is it different from AngularJS?
- What is a single-page application (SPA)?
- What does the Angular CLI do? Name some common commands.
- What is a standalone component, and why did Angular move away from NgModules?
- What is TypeScript, and why does Angular use it?
- Describe the structure of a basic Angular project.