Bird
0
0

You want to inject two different services, ApiService and LoggerService, into a component and use them in ngOnInit. Which is the correct way to do this?

hard📝 component behavior Q8 of 15
Angular - Services and Dependency Injection
You want to inject two different services, ApiService and LoggerService, into a component and use them in ngOnInit. Which is the correct way to do this?
AUse @ViewChild to get references to both services
BCreate new instances of both services inside ngOnInit
CInject only one service and import the other directly in ngOnInit
DInject both in constructor with private modifiers, then call their methods inside ngOnInit
Step-by-Step Solution
Solution:
  1. Step 1: Understand Angular injection for multiple services

    Multiple services can be injected by listing them as constructor parameters with access modifiers.
  2. Step 2: Use injected services in lifecycle hooks

    Injected services are accessible via this and can be used inside ngOnInit.
  3. Final Answer:

    Inject both in constructor with private modifiers, then call their methods inside ngOnInit -> Option D
  4. Quick Check:

    Inject multiple services via constructor, use in ngOnInit [OK]
Quick Trick: Inject multiple services via constructor parameters [OK]
Common Mistakes:
MISTAKES
  • Creating new service instances manually
  • Importing services instead of injecting
  • Using @ViewChild for services

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes