Angular · Senior · Question 51 of 75
Why does takeUntilDestroyed() throw when called inside a click handler?
onClick() {
this.data$.pipe(takeUntilDestroyed()).subscribe();
}- ✓Correct answer: Without arguments it needs an injection context; outside it, pass a
DestroyRefinjected earlier - 2It works only with HTTP Observables
- 3Click handlers run outside Angular
Explanation
Inject DestroyRef in a field and call takeUntilDestroyed(this.destroyRef). The same rule applies to toSignal and effect.