Bird
0
0

Which of the following is the correct way to inject a service named DataService into an Angular component's constructor to facilitate testing?

easy📝 Syntax Q3 of 15
Angular - Services and Dependency Injection
Which of the following is the correct way to inject a service named DataService into an Angular component's constructor to facilitate testing?
Aconstructor(private dataService) {}
Bconstructor(dataService: DataService) { this.dataService = dataService; }
Cconstructor(@Inject(DataService) dataService) {}
Dconstructor(private dataService: DataService) {}
Step-by-Step Solution
Solution:
  1. Step 1: Recognize Angular DI syntax

    Angular uses constructor parameter properties with access modifiers to inject services.
  2. Step 2: Analyze options

    constructor(private dataService: DataService) {} correctly uses private to declare and inject DataService.
  3. Final Answer:

    constructor(private dataService: DataService) {} -> Option D
  4. Quick Check:

    Private constructor parameter triggers DI injection [OK]
Quick Trick: Use private in constructor to inject services [OK]
Common Mistakes:
MISTAKES
  • Omitting access modifier prevents DI injection
  • Using @Inject without type annotation incorrectly
  • Not assigning injected service to a class property

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes