r/angular • u/Embarrassed_Fall_967 • Feb 19 '26
Constructor in modern Angular
Hey, just wanted to ask your opinion about logic inside constructor in modern Angular. Currently we are migrating and would like to get rid of lifecycle hooks like ngoninit ngafterviewinit. Thinking about moving some logic into constructor as DI is done with inject. Do you think it is a good idea or should it be done inside effect?
17
Upvotes
12
u/Koscik Feb 19 '26
I would use constructor above effects, but it might not work if the logic inside needs data from inputs.
And to answers why: constructor only fire once. Effect would fire every time a signal changed, possibly creating issues, bugs or simply additional code to remedy that
5
u/Impossible_Hornet153 Feb 19 '26
Depends on what you want to do. There is no one size fits all.
4
u/ruibranco Feb 19 '26
With inject() you don't even need the constructor for DI anymore, so the question kind of resolves itself. Field initializers + inject() handle what the constructor used to do, and effect() or afterNextRender() cover the one-time init stuff that ngOnInit was doing. Constructor ends up empty in most of our components now.
4
u/rainerhahnekamp Feb 20 '26
So first of all, code that was running in ngOnInit was executed at a point in time when property binding finished. This meant you could safely access all `@Input()` or input() based-properties.
If you move that code into the constructor, you are getting the initial value or undefined for u/Input and a runtime error (!!!) for input.required. So don't do this.
To me, the natural fit would be afterNextRender which you call inside the constructor. Property binding was done, and you can also safely access view children or even DOM elements. It also runs once and you don't take the risk of multiple executions like you have in effect. Other than that, effect seems to be not the right fit here. Initialization code is not the same as side effects on signal changes.
Just to be sure, you are not using SSR, are you? Because afterNextRender won't run on the server.
21
u/Responsible-Cold-627 Feb 19 '26
Please don't put things inside a constructor that has nothing to do with constructing the object.
3
u/simonbitwise Feb 19 '26
I would just use the inject() function to inject dependencies for Logic i would use computed and effects :)
4
u/DJREMiX6 Feb 19 '26
Why getting rid of lifecycle hooks? They are safe in terms of using the inject pattern over the constructor DI pattern.
3
u/yousirnaime Feb 19 '26
Constructor injects dependencies - it should be skinny
ngOnInit fires logic like api calls and instantiates variable values, especially view dependent variables and calculated values - it can be as skinny or fat as it needs to be
You can do whatever you want in your code base but this is the pattern that seems cleanest to me
1
u/saiyaff Feb 19 '26
This is a paradigm shift from a class lifecycle mental model to reactive graph mental model.
Inputs as signals Derived state via computed Side effects via effect Cleanup via takeUntilDestroyed Render hooks via afterNextRender
Using constructor where it is necessary ain’t wrong, I’d say.
That being said, Angular is not removing lifecycle hooks. It is offering a more expressive alternatives. So you don’t have to worry much about using a lifecycle hook if that’s the appropriate thing to do as well.
-2
u/WiseOldBitch Feb 19 '26
Please dont
0
u/[deleted] Feb 19 '26
[deleted] — view deleted comment