Bird
0
0

Identify the error in this Angular service declaration:

medium📝 Debug Q6 of 15
Angular - Services and Dependency Injection
Identify the error in this Angular service declaration:
@Injectable({ providedIn: 'component' })
export class MyService {}
A'component' is not a valid value for providedIn.
BThe service must be declared in a module's providers array.
CThe service cannot be injectable.
DThe service must use providedIn: 'root' only.
Step-by-Step Solution
Solution:
  1. Step 1: Check valid providedIn values

    Angular supports 'root', 'platform', 'any', or a module class for providedIn, but not 'component'.
  2. Step 2: Understand component scope provision

    Component scope is set via component's providers array, not via providedIn.
  3. Final Answer:

    'component' is not a valid value for providedIn. -> Option A
  4. Quick Check:

    providedIn accepts only root, platform, any, or module [OK]
Quick Trick: providedIn cannot be 'component'; use providers array instead [OK]
Common Mistakes:
MISTAKES
  • Using 'component' as providedIn value
  • Thinking providedIn controls component scope
  • Assuming service must be in module providers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes