Bird
0
0

Examine the following reactive form code snippet and identify the mistake:

medium📝 Debug Q6 of 15
Angular - Reactive Forms
Examine the following reactive form code snippet and identify the mistake:
this.form = new FormGroup({
  email: new FormControl('')
});
this.form.controls.email = new FormControl('test@example.com');
ADirectly assigning a new FormControl to a control property is incorrect; use setValue or patchValue instead.
BFormControl should be imported from '@angular/forms'.
CFormGroup cannot contain only one FormControl.
DThe initial value of FormControl cannot be an empty string.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the code

    The code tries to replace the 'email' control by direct assignment.
  2. Step 2: Understand reactive form control management

    Controls should not be replaced by assignment; instead, update values using setValue or patchValue.
  3. Final Answer:

    Directly assigning a new FormControl to a control property is incorrect; use setValue or patchValue instead. -> Option A
  4. Quick Check:

    Never replace controls by assignment, update values properly [OK]
Quick Trick: Update control values, don't replace controls directly [OK]
Common Mistakes:
MISTAKES
  • Replacing controls by assignment instead of updating values
  • Confusing control initialization with value setting

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes