Bird
0
0

Which is the correct way to provide a service only in a specific Angular component?

easy📝 Syntax Q3 of 15
Angular - Services and Dependency Injection
Which is the correct way to provide a service only in a specific Angular component?
A@Injectable({ providedIn: 'root' })
BAdd the service to the component's <code>providers</code> array
CAdd the service to the module's <code>providers</code> array
DDeclare the service inside the component's class
Step-by-Step Solution
Solution:
  1. Step 1: Identify component-level service provision

    To provide a service only in a component, add it to that component's providers array.
  2. Step 2: Understand other options

    Providing in root or module scopes shares the service more widely; declaring inside class is invalid.
  3. Final Answer:

    Add the service to the component's providers array -> Option B
  4. Quick Check:

    Component providers array = component-only service [OK]
Quick Trick: Use component's providers array for component-only service [OK]
Common Mistakes:
MISTAKES
  • Using providedIn: 'root' for component-only scope
  • Adding service to module providers instead
  • Trying to declare service inside component class

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes