Bird
0
0

Identify the error in this component code:

medium📝 Debug Q6 of 15
Angular - RxJS and Observables Fundamentals
Identify the error in this component code:
data$ = of('Test');
ngOnInit() {
this.data$.subscribe(value => console.log(value));
}

Template:
<div>{{ data$ | async }}</div>
ATemplate syntax is incorrect; async pipe needs parentheses
BAsync pipe cannot be used with Observables declared in ngOnInit
CManual subscription causes memory leak; async pipe already subscribes
DObservable must be converted to Promise for async pipe
Step-by-Step Solution
Solution:
  1. Step 1: Analyze manual subscription

    The component manually subscribes to data$ in ngOnInit, which is unnecessary if async pipe is used.
  2. Step 2: Understand async pipe auto subscription

    The async pipe automatically subscribes and unsubscribes, so manual subscription risks memory leaks.
  3. Final Answer:

    Manual subscription causes memory leak; async pipe already subscribes -> Option C
  4. Quick Check:

    Manual subscribe + async pipe = memory leak risk [OK]
Quick Trick: Avoid manual subscribe when using async pipe [OK]
Common Mistakes:
MISTAKES
  • Thinking async pipe needs parentheses
  • Believing async pipe only works with Promises
  • Ignoring memory leaks from manual subscriptions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes