Bird
0
0

Find the problem in this service usage:

medium📝 Debug Q7 of 15
Angular - Services and Dependency Injection
Find the problem in this service usage:
@Injectable({ providedIn: 'root' })
export class LoggerService { log(msg: string) { console.log(msg); } }
@Component({ selector: 'app-log', template: '' })
export class LogComponent {
constructor() {
this.logger.log('Test');
}
private logger: LoggerService;
}
Alog method should be static
BLoggerService should not be providedIn root
CLoggerService is not injected via constructor parameter
DComponent selector is missing
Step-by-Step Solution
Solution:
  1. Step 1: Check how LoggerService is accessed

    The component tries to use this.logger without injecting it.
  2. Step 2: Understand Angular injection pattern

    Services must be injected via constructor parameters to be available.
  3. Final Answer:

    LoggerService is not injected via constructor parameter -> Option C
  4. Quick Check:

    Service must be injected in constructor = A [OK]
Quick Trick: Inject services via constructor parameters to use them [OK]
Common Mistakes:
MISTAKES
  • Declaring service as class property without injection
  • Making service methods static unnecessarily
  • Ignoring constructor injection pattern

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes