Bird
0
0

Which template snippet correctly implements this?

hard📝 component behavior Q8 of 15
Angular - Template-Driven Forms
You want to display a custom error message in Angular when a form control named 'password' is invalid due to either being required or having a minlength error, but only after the user has touched the field. Which template snippet correctly implements this?
A<div *ngIf="passwordControl.touched && (passwordControl.hasError('required') || passwordControl.hasError('minlength'))">Password is required and must be at least 8 characters.</div>
B<div *ngIf="passwordControl.errors.required || passwordControl.errors.minlength">Password is invalid.</div>
C<div *ngIf="passwordControl.invalid && passwordControl.dirty">Password error.</div>
D<div *ngIf="passwordControl.touched && passwordControl.errors.required && passwordControl.errors.minlength">Password error.</div>
Step-by-Step Solution
Solution:
  1. Step 1: Condition for showing error

    Show error only if passwordControl.touched is true and either required or minlength error exists.
  2. Step 2: Evaluate options

    <div *ngIf="passwordControl.touched && (passwordControl.hasError('required') || passwordControl.hasError('minlength'))">Password is required and must be at least 8 characters.</div> uses hasError() and combines conditions correctly with touched.
  3. Final Answer:

    <div *ngIf="passwordControl.touched && (passwordControl.hasError('required') || passwordControl.hasError('minlength'))">Password is required and must be at least 8 characters.</div> correctly implements the required logic.
  4. Quick Check:

    Use hasError() with touched for precise error display [OK]
Quick Trick: Use hasError() and touched to show specific errors [OK]
Common Mistakes:
MISTAKES
  • Checking errors without verifying touched
  • Using && instead of || for multiple errors
  • Accessing errors object directly without null checks

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes