Bird
0
0

Given a service provided in providedIn: 'root' and also listed in a lazy-loaded module's providers array, what instance behavior occurs?

medium📝 state output Q4 of 15
Angular - Services and Dependency Injection
Given a service provided in providedIn: 'root' and also listed in a lazy-loaded module's providers array, what instance behavior occurs?
AOnly one instance is created app-wide from root scope.
BTwo instances are created: one root-wide and one for the lazy module.
CNo instance is created due to conflict.
DThe service is only available in the lazy-loaded module.
Step-by-Step Solution
Solution:
  1. Step 1: Understand root and lazy module scopes

    Providing a service in root creates a singleton app-wide instance. Providing it again in a lazy-loaded module creates a separate instance scoped to that module.
  2. Step 2: Analyze instance count

    Because the lazy module has its own provider, it overrides root for that module, resulting in two instances.
  3. Final Answer:

    Two instances are created: one root-wide and one for the lazy module. -> Option B
  4. Quick Check:

    Lazy module providers override root, creating separate instances [OK]
Quick Trick: Lazy module providers override root, creating separate instances [OK]
Common Mistakes:
MISTAKES
  • Assuming only one instance app-wide
  • Thinking no instance is created
  • Confusing lazy module scope with eager module

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes