Bird
0
0

Given this Angular component code snippet, what will be logged to the console?

medium📝 component behavior Q13 of 15
Angular - Services and Dependency Injection
Given this Angular component code snippet, what will be logged to the console?
import { Component, inject } from '@angular/core';
import { LoggerService } from './logger.service';

@Component({
  selector: 'app-sample',
  standalone: true,
  template: `

Check console

`, providers: [LoggerService] }) export class SampleComponent { private logger = inject(LoggerService); constructor() { this.logger.log('Hello from SampleComponent'); } }
ALogs 'Hello from SampleComponent' to the console
BThrows an error because LoggerService is not provided
CLogs nothing because log() is not called
DLogs 'undefined' because logger is not initialized
Step-by-Step Solution
Solution:
  1. Step 1: Check service injection and provider

    The component provides LoggerService, so inject(LoggerService) works correctly.
  2. Step 2: Check constructor behavior

    The constructor calls this.logger.log() with the message, so it logs to console.
  3. Final Answer:

    Logs 'Hello from SampleComponent' to the console -> Option A
  4. Quick Check:

    Service provided + log called = console output [OK]
Quick Trick: Service must be provided to inject and use it [OK]
Common Mistakes:
MISTAKES
  • Forgetting to add service to providers array
  • Assuming inject() fails without constructor
  • Thinking log() is not called

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes