Bird
0
0

Consider this Angular code snippet:

medium📝 component behavior Q5 of 15
Angular - Template-Driven Forms
Consider this Angular code snippet:
this.passwordControl = new FormControl('', [Validators.required, Validators.minLength(6)]);

Which condition correctly shows the error message 'Password too short' only when the password is invalid due to length and the user has interacted with the field?
ApasswordControl.invalid && passwordControl.pristine
BpasswordControl.hasError('minlength') && passwordControl.touched
CpasswordControl.errors.minlength && passwordControl.dirty
DpasswordControl.hasError('required') && passwordControl.touched
Step-by-Step Solution
Solution:
  1. Step 1: Identify how to check specific validation error

    Use hasError('minlength') to check if the minlength validator failed.
  2. Step 2: Combine with user interaction check

    Use touched to ensure the user has left the input before showing the error.
  3. Final Answer:

    passwordControl.hasError('minlength') && passwordControl.touched -> Option B
  4. Quick Check:

    Use hasError('minlength') and touched to show length error [OK]
Quick Trick: Use hasError('minlength') with touched for length errors [OK]
Common Mistakes:
MISTAKES
  • Checking errors.minlength directly without null check
  • Using pristine instead of touched
  • Showing required error instead of minlength

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes