Bird
0
0

Identify the error in this service injection code:

medium📝 Debug Q6 of 15
Angular - Services and Dependency Injection
Identify the error in this service injection code:
@Component({ selector: 'app-test', template: '' })
export class TestComponent {
constructor(service: DataService) {}
}
export class DataService {}
AConstructor parameter should be private or public
BDataService is not marked with @Injectable decorator
CService should be injected using @Inject decorator
DComponent selector is invalid
Step-by-Step Solution
Solution:
  1. Step 1: Check service class declaration

    DataService lacks the @Injectable decorator needed for DI.
  2. Step 2: Understand Angular DI requirements

    Services must be decorated with @Injectable to be injectable.
  3. Final Answer:

    DataService is not marked with @Injectable decorator -> Option B
  4. Quick Check:

    Missing @Injectable causes injection error = C [OK]
Quick Trick: Always add @Injectable to services for injection [OK]
Common Mistakes:
MISTAKES
  • Forgetting @Injectable on service classes
  • Thinking constructor param visibility is mandatory
  • Confusing @Inject with @Injectable

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes