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());

Explanation

DestroyRef belongs to the current injector: a component, a route's environment, or the app. takeUntilDestroyed is built on it.

Further reading

Problem with a question

Angular · Middle · question 61

Test yourself

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

Take this quiz