Question presented to candidate: "Three components on our dashboard each fetch and filter notifications themselves, and they keep drifting out of sync. What is an Angular service, and how would one fix this?"
What a strong answer should cover:
- A service is a plain class that holds logic or state that does not belong to one view, made available through dependency injection.
- Why: one source of truth for shared state, reusable business rules, thin components, and an easy seam for test fakes.
- How it is registered:
@Injectable({ providedIn: 'root' })for an app-wide singleton, or the newer@Service()decorator (Angular 22.1+), which is root-provided by default and only supportsinject(). - How it is consumed:
inject(MyService)in a component, directive, pipe, guard or another service. - Good practice: expose read-only state (a readonly signal or observable) and mutate only through service methods.
- Scope matters: root gives one shared instance; a component
providersarray gives each component its own.
Clarifying questions expected:
- "Should all three components see the same notifications, or does each need its own list?"
- "Is the data local state, or does it come from an API that should be cached?"
Code / implementation expected: Yes β a small service with state and methods, injected into two or more components.