Angular · Middle · Question 61 of 75
How do you register cleanup code from a service or a function without ngOnDestroy?
const destroyRef = inject(DestroyRef);
destroyRef.onDestroy(() => socket.close());- ✓Correct answer: Inject
DestroyRefand callonDestroy - 2Use
window.onunload - 3Services are never destroyed
Explanation
DestroyRef belongs to the current injector: a component, a route's environment, or the app. takeUntilDestroyed is built on it.