Bird
0
0

Which of the following is the correct syntax to inject AuthService into UserService constructor?

easy📝 Syntax Q3 of 15
Angular - Services and Dependency Injection
Which of the following is the correct syntax to inject AuthService into UserService constructor?
Aconstructor(@Inject AuthService authService) {}
Bconstructor(private authService: AuthService) {}
Cconstructor(authService = new AuthService()) {}
Dconstructor(AuthService authService) {}
Step-by-Step Solution
Solution:
  1. Step 1: Recall Angular constructor injection syntax

    Angular uses TypeScript constructor parameters with access modifiers to inject services.
  2. Step 2: Evaluate options

    constructor(private authService: AuthService) {} uses correct syntax with private modifier and type. constructor(AuthService authService) {} misses access modifier, C manually creates instance (not injection), A uses incorrect decorator syntax.
  3. Final Answer:

    constructor(private authService: AuthService) {} -> Option B
  4. Quick Check:

    Constructor injection syntax = private param with type [OK]
Quick Trick: Use private/public in constructor for injection [OK]
Common Mistakes:
MISTAKES
  • Omitting access modifier in constructor
  • Manually creating service instance inside constructor
  • Using @Inject decorator incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes