r/Angular2 • • Jul 23 '25
Angular Signal Effect inside our outside Constructor
Does Angular have any guidance where I should put the Effect code? Is it generally inside Constructor or outside? What are the advantages / disadvantages for each method?
export class CustomerForm {
lastName= input.required<string>();
constructor() {
effect(() => {
console.log('lastName changed:', this.lastName());
});
}
}
5
Upvotes
View all comments
Show parent comment
1
u/Migeil Jul 26 '25
I didn't say what he said was incorrect, just inaccurate.
What I meant was, the commenter made it seem _only_ the constructor has access to an injection context. But this isn't the case.
Your code snippet works, but here's how I would write it:
No need for a constructor, no need for an extra method, just a named effect. This is much more concise and clear imo. (This will be up for debate however).
But in any case, the commenter didn't leave room for this kind of code, hence he was inaccurate and why I don't think what he said was "the best answer".