Bird
0
0

Identify the error in this Angular template snippet for form validation:

medium📝 Debug Q14 of 15
Angular - Template-Driven Forms
Identify the error in this Angular template snippet for form validation:
<input type="email" name="email" ngModel required #emailRef>
<div *ngIf="emailRef.invalid && emailRef.touched">
  <small *ngIf="emailRef.errors.required">Email is required.</small>
  <small *ngIf="emailRef.errors.email">Invalid email format.</small>
</div>
AThe input type should be "text" instead of "email"
BThe required attribute should be [required]="true"
CThe *ngIf conditions should use OR (||) instead of AND (&&)
DMissing template reference variable export like #emailRef="ngModel"
Step-by-Step Solution
Solution:
  1. Step 1: Check template reference variable usage

    The template reference variable #emailRef is used to access validation state, but it lacks the export ="ngModel" which is needed to access invalid and errors.
  2. Step 2: Validate other parts

    The required attribute is correct as is, AND logic for showing errors is appropriate, and input type "email" is valid for email validation.
  3. Final Answer:

    Missing template reference variable export like #emailRef="ngModel" -> Option D
  4. Quick Check:

    Template ref must export ngModel for validation access [OK]
Quick Trick: Always export ngModel in template ref to access validation state [OK]
Common Mistakes:
MISTAKES
  • Omitting ="ngModel" in template reference variable
  • Changing required attribute to binding unnecessarily
  • Misusing logical operators in *ngIf
  • Changing input type incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes