Bird
0
0

You wrote this reactive form code but get an error: form.controls['name'] is undefined. What is the likely cause?

medium📝 Debug Q6 of 15
Angular - Reactive Forms
You wrote this reactive form code but get an error: form.controls['name'] is undefined. What is the likely cause?
this.form = new FormGroup({ email: new FormControl('') }); console.log(this.form.controls['name'].value);
AYou must import FormsModule for reactive forms
BFormGroup should be initialized in ngOnInit
CThe 'name' control was not defined in the FormGroup
DThe control name must be 'email' not 'name'
Step-by-Step Solution
Solution:
  1. Step 1: Check FormGroup controls

    The FormGroup only defines 'email', no 'name' control exists.
  2. Step 2: Accessing undefined control causes error

    Trying to access 'name' control causes undefined error.
  3. Final Answer:

    The 'name' control was not defined in the FormGroup -> Option C
  4. Quick Check:

    Undefined control access = error [OK]
Quick Trick: Define all controls before accessing them [OK]
Common Mistakes:
MISTAKES
  • Assuming FormsModule is needed for reactive forms
  • Thinking initialization timing causes this error
  • Confusing control names

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes