#should we use inject vs constructor injection ie: dependency injection? in modern angular?

14 messages · Page 1 of 1 (latest)

shrewd pelican
#

can anyone tell me if it would be reasonable to do so or is there still a place in angular for the normal dependency injection?

finite sand
#

Angular DI is getting a new recommendation in the upcoming 2025 style guide: prefer the inject() function over constructor dependency injection. This shift is partly due to how native class fields behave in modern JavaScript. The inject() function also improves code readability and type inference.

Constructor injection is still fully supported...

▶ Play video
#

a bit late but just watched this and it sums it up perfectly with visuals

shrewd pelican
shrewd pelican
shrewd pelican
grim finch
#

In most cases I would choose inject() over anything else

dawn grove
#

for me personally i prefer constructor injection.
initialisation of a parameter before the constructor lead to bad design - in every language works that implement OOP.

grim finch
#

@dawn grove I thought parameter initialisation is happening at the same time as in constructor kinda?

dawn grove
#

I'm pretty sure about that, first starting parameters and after that the constructor.

Constructor Timing: The constructor runs after the properties are initialized.

shrewd pelican
#

Angular is going towards functional programmimg and not oop so i think i will go with inject it does everything i need (and angular is heading away from oop) react also

dawn grove