Bird
0
0

This Angular code tries to implement the Observer pattern but has a bug:

medium📝 Debug Q14 of 15
Angular - Advanced Patterns
This Angular code tries to implement the Observer pattern but has a bug:
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';

@Injectable({ providedIn: 'root' })
export class LoggerService {
  private logSubject = new Subject();
  log$ = this.logSubject.asObservable();

  logMessage(message: string) {
    this.logSubject.next;
  }
}

What is the bug and how to fix it?
AThe method calls <code>next</code> without parentheses; fix by adding <code>()</code>.
BThe Subject should be public, not private.
CThe service should not use Subject but BehaviorSubject.
DThe @Injectable decorator is missing a providedIn property.
Step-by-Step Solution
Solution:
  1. Step 1: Identify the incorrect method call

    The code uses this.logSubject.next; which does not call the function.
  2. Step 2: Correct the method call syntax

    It should be this.logSubject.next(message); with parentheses and argument.
  3. Final Answer:

    The method calls next without parentheses; fix by adding (). -> Option A
  4. Quick Check:

    Method call needs parentheses [OK]
Quick Trick: Method calls need () to execute [OK]
Common Mistakes:
  • Ignoring missing parentheses on method calls
  • Changing Subject visibility unnecessarily
  • Replacing Subject with BehaviorSubject without reason

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes