Hi guys I am trying to create a new FormGroup inside an effect hook but somehow I am getting an error that the FormGroup still expects an instance. But the error doesn't appear if it's inside the constructor but outside the effect hook.
In my work project I am injecting a signal store to the class and based on the updated signal I want the effect to fire and to update the FormGroup. Is this common behaviour?
Minimal reproduction:
@Component({
selector: 'app-root',
standalone: true,
imports: [ReactiveFormsModule],
templateUrl: './app.component.html',
styleUrl: './app.component.css'
})
export class AppComponent {
form!: FormGroup;
constructor(private fb: FormBuilder){
//Outside effect Hook this.initForm() is triggered but not inside the effect Hook why?
//this.initForm()
effect(() => {
this.initForm()
})
}
initForm(){
this.form = this.fb.group({
name: [""]
})
}
}
Error:
ERROR Error: NG01052: formGroup expects a FormGroup instance. Please pass one in.
Example:
<div [formGroup]="myGroup">
<input formControlName="firstName">
</div>
In your class:
this.myGroup = new FormGroup({
firstName: new FormControl()
});