Bird
0
0

Why does this Angular component fail to inject the service?

medium📝 Debug Q7 of 15
Angular - Services and Dependency Injection
Why does this Angular component fail to inject the service?
@Component({selector: 'app', template: ''})
export class MyComponent {
  constructor(private dataService: DataService) {}
}

Given DataService is not provided anywhere.
ADataService class is missing @Component decorator
BDataService is not registered as a provider
CConstructor parameter is missing private keyword
DComponent selector is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Check service registration

    DataService must be registered as a provider in a module or via providedIn for DI to work.
  2. Step 2: Verify other options

    Selector and private keyword are correct; DataService should have @Injectable, not @Component.
  3. Final Answer:

    DataService is not registered as a provider -> Option B
  4. Quick Check:

    Service must be provided to be injectable [OK]
Quick Trick: Register services as providers or use providedIn [OK]
Common Mistakes:
MISTAKES
  • Forgetting to provide the service
  • Confusing @Component with @Injectable
  • Ignoring private keyword importance

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes