Bird
0
0

You created a service with @Injectable() but forgot to add providedIn: 'root'. What problem will occur when multiple components inject this service?

medium📝 Debug Q14 of 15
Angular - Services and Dependency Injection
You created a service with @Injectable() but forgot to add providedIn: 'root'. What problem will occur when multiple components inject this service?
AAngular will throw a compile-time error.
BThe service will not be injectable anywhere.
CThe service will be singleton by default anyway.
DAngular will create separate instances for each component, breaking singleton behavior.
Step-by-Step Solution
Solution:
  1. Step 1: Understand default service provider behavior

    Without providedIn: 'root', Angular does not provide the service globally.
  2. Step 2: Effect on multiple injections

    Each component that provides the service separately will get its own instance, so no singleton.
  3. Final Answer:

    Angular will create separate instances for each component, breaking singleton behavior. -> Option D
  4. Quick Check:

    Missing providedIn: 'root' = multiple instances [OK]
Quick Trick: No providedIn: 'root' means no singleton [OK]
Common Mistakes:
MISTAKES
  • Assuming singleton is default without providedIn
  • Expecting compile errors for missing providedIn
  • Thinking service becomes unusable

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes