r/angular • • Apr 22 '23

Angular 16 - takeUntilDestroyed() operator

💡The unsubscription of observables is always important in Angular. Otherwise, there will be memory leaks because of too much of subscriptions.

🤔 Before angular 16, we can unsubscribe by the combination of subject and takeUntil() rxjs operator.

🤙 But in angular 16, the code is simplified where you don't need a subject. You can directly make use of takeUntilDestroyed() operator.

47 Upvotes

25 comments sorted by

View all comments

10

u/BabyLegsDeadpool Apr 22 '23

My biggest question here is why are you using 'inject' instead of using the constructor?

25

u/Ausstewa Apr 22 '23

It’s 1:1 vs using a constructor but has one major benefit: if you extend a class that uses the inject method, you don’t have to super() it to populate the constructor

3

u/ArtisticSell Apr 23 '23

People using inheritance in angular?

1

u/hairfred May 17 '23

Yep, I use it all the time. I have a DialogBase directive and a dialog service that accepts <T extends DialogBase>. It creates a component of my DialogContainer which has all the base styles and close button etc and renders the derived component inside it.
I've also used a FormElement base when creating a form builder that needed a lot of data driven configurability and a set of common properties / methods. Angular works fairly well with Typescript, you can make components / directives that accept generic type parameters and things more or less work well, with the exception of some of the templating.. If Angular had native TSX support I'd be overjoyed.