How to set FormGroup and FormControl at the ngOnInit() with Angular?

You can use FormBuilder to help with this

Steps to Implement

  • Inject FormBuilder
constructor (private fb: FormBuilder) {}
  • Call .group() function on the injected service to generate the FormGroup. This way you do not have to use new and everything works
ngOnInit() {
  this.form = this.fb.group({
    email: [null, [Validators.required, Validators.email]],
    password: [null, [Validators.required, Validators.minLength(6)]]
  })
}

See sample demo

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top