Bird
0
0

You have an Angular service declared as:

medium📝 Debug Q6 of 15
Angular - Services and Dependency Injection
You have an Angular service declared as:
@Injectable()
export class AuthService {}

What issue arises if you want this service to be a singleton but do not specify providedIn or add it to a module's providers?
AAngular will not provide the service anywhere, causing injection errors
BThe service will automatically be singleton without any configuration
CMultiple instances will be created if added to different component providers
DThe service will be tree-shaken and removed from the build
Step-by-Step Solution
Solution:
  1. Step 1: No providedIn or module provider

    Without providedIn or module-level provider, Angular does not know where to provide the service.
  2. Step 2: Adding service to component providers creates multiple instances

    If added to multiple components' providers, each gets its own instance, breaking singleton behavior.
  3. Final Answer:

    Multiple instances will be created if added to different component providers -> Option C
  4. Quick Check:

    Missing providedIn or module provider breaks singleton [OK]
Quick Trick: No providedIn or module provider means no singleton [OK]
Common Mistakes:
MISTAKES
  • Assuming service is singleton by default
  • Not realizing component providers create separate instances

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes