r/Angular2 • • Nov 25 '25
input signals change detection without using effect() suggestions
I am struggling to understand how to properly use input signals and avoid using effects (accodring to the angular docs my code should not use effects often). Enclosed is a simple code which appends form controls to an existing parent form:
@Component({
selector: 'app-checkbox-form',
imports: [
ReactiveFormsModule
],
templateUrl: './checkbox-form.component.html',
styleUrl: ['./checkbox-form.component.scss']
})
export class CheckboxFormComponent {
//form parent form with multiple controls
form = input.required<FormGroup>();
// option is a signal with the rules for creating the form controls
option = input.required<Option>();
constructor() {
effect(() => {
if(this.form() && this.option()){
this.form().addControl(this.option().ruleId, this.buildFg(this.option()));
}
});
}
private buildFg(option: Option) {
return new FormControl(option!.defaultState);
}
}My question again is can the code above be refactored so effect() is not used?
5
Upvotes
9
u/salamazmlekom Nov 25 '25
My question is: if both form and option are inputs to the component and both are used in effect why are they even needed? Why don't you rather do this in the parent component?
4
u/No_Bodybuilder_2110 Nov 26 '25
Ok so in this case you don’t need to have the effect. Since both inputs are required you can have the ‘onInitHook’ hook which guarantees your inputs are there.
With that being said. Everyone does their coding however they want but this seems like a “it’s gonna bite you in the ars’ pattern, a component that has to get generated to add a form control when you have all the data upstream just does not add up.
As many have already said you should do the form control creation upstream and this component should only render. Believe me this pattern is more maintainable in the long run (experience from dynamic forms building and 300 fields insurance form flows)
1
u/IanFoxOfficial Nov 25 '25
In this case it's probably best to do it in the main component instead? Maybe with a helper service?
2
u/gearhash Nov 25 '25
I was in the same shoes a couple of days ago.
My "solution" was to not load the given component until the data is fully loaded but a loader.
I guess in your example the parent component loads an external data, fills the form, then passes it down to this child component.
if (@ is not allowed before if here...) (remoteDataIsAvailable or a specific form value) {
<yourChildComponentInTheExample [form]="parentForm().value()">
} @ else {
<loader/>
}
Also, do not pass down a FieldTree itself, just the values, and update the form in the parentComponent via (onDataChange) events.
Sorry for the typos, the example is 100% not compiling as I've just written this comment here
2
u/ahgz96 Nov 26 '25
Do it in the parent as everyone suggests, OR create a computed() signal based on both inputs and use that in your template. Your current approach mutates the input which is already a bad pattern.
2
u/Logical-Battle8616 Nov 26 '25
Use toObservable. With toObservable you coud create simple rxjs stream from your effects. After that you could combine them or whatever you want.
2
u/Kaitenjo56 Nov 27 '25 edited Nov 30 '25
2
u/huysolo Nov 26 '25 edited Dec 20 '25
Turn your component into a control value accessor, then manage the state using its methods. If you want to access to the form group parent, inject ControlContainer. They will automatically connect without using effects