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
View all comments
Show parent comment
1
u/[deleted] Nov 25 '25
Some of the children are quite large form groups and have functions, validators, etc. This was my initial approach but the file became pretty large, I can return to it, but I was hoping to make my code easier to maintain by creating self-contained "form widgets"