Bird
0
0

Examine this Angular component code:

medium📝 Debug Q6 of 15
Angular - Services and Dependency Injection
Examine this Angular component code:
import { Component } from '@angular/core';
import { LoggingService } from './logging.service';

@Component({ selector: 'app-log', template: '' })
export class LogComponent {
  constructor(private loggingService) {}
}

What is the issue with this code?
AThe component is missing the @Injectable decorator
BThe constructor parameter lacks a type annotation for LoggingService
CThe LoggingService is not imported correctly
DThe selector property in @Component is invalid
Step-by-Step Solution
Solution:
  1. Step 1: Check constructor parameters

    The constructor parameter 'loggingService' lacks a type annotation.
  2. Step 2: Importance of typing

    Angular DI requires the type to know which service to inject.
  3. Step 3: Other options

    @Injectable is not required on components; import and selector appear correct.
  4. Final Answer:

    The constructor parameter lacks a type annotation for LoggingService -> Option B
  5. Quick Check:

    Constructor parameters must be typed for DI [OK]
Quick Trick: Always type constructor parameters for DI [OK]
Common Mistakes:
MISTAKES
  • Omitting type annotations in constructor
  • Confusing @Injectable necessity on components
  • Assuming import errors without checking

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes