0

***Error: src/app/r-form-example/r-form-example.component.ts:11:3 - error TS2564: Property 'registrationForm' has no initializer and is not definitely assigned in the constructor.

11 registrationForm : FormGroup;

The above error i am getting.


import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';

@Component({
  selector: 'app-r-form-example',
  templateUrl: './r-form-example.component.html',
  styleUrls: ['./r-form-example.component.css']
})
export class RFormExampleComponent implements OnInit {

  registrationForm : FormGroup;

  constructor( private fb : FormBuilder) {
   }

  ngOnInit(): void {
    // this.registrationForm = new FormGroup({
    //   'firstName': new FormControl(),
    //   'lastName': new FormControl(),
    // })
    this.registrationForm = this.fb.group({
      'firstName': new FormControl(),
      'lastName':new FormControl()
    });
  }
  
  addRegistration(){
    console.log(this.registrationForm.value);
  }

}
janaki_k
  • 3
  • 3
  • registrationForm! : FormGroup; – MikeOne Jul 04 '21 at 09:34
  • 2
    Does this answer your question? [Property '...' has no initializer and is not definitely assigned in the constructor](https://stackoverflow.com/questions/49699067/property-has-no-initializer-and-is-not-definitely-assigned-in-the-construc) – Yong Shun Jul 04 '21 at 09:35
  • try this! registrationForm!: FormGroup; – Sheno Navy Jul 04 '21 at 09:58

1 Answers1

1

on your typescript settings - you can't declare variable without assign value to it (when you declare it or in the constructor..). you can declare it like this :

 registrationForm!: FormGroup

or assign the value in the constructor

Eli Porush
  • 1,339
  • 1
  • 7
  • 18