Angular · Senior · Question 51 of 75

Why does takeUntilDestroyed() throw when called inside a click handler?

onClick() {
  this.data$.pipe(takeUntilDestroyed()).subscribe();
}

Explanation

Inject DestroyRef in a field and call takeUntilDestroyed(this.destroyRef). The same rule applies to toSignal and effect.

Further reading

Problem with a question

Angular · Senior · question 51

Test yourself

Take the Angular · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz