Bird
0
0

In this code snippet:

medium📝 Debug Q7 of 15
Angular - Reactive Forms
In this code snippet:
this.form = new FormGroup({
age: new FormControl(25)
});
this.form.controls.age.markAsPristine();
console.log(this.form.controls.age.pristine);

The console logs false. Why?
AmarkAsPristine() was not called correctly
BThe control value was changed after initialization
CmarkAsPristine() sets pristine to true, so false is unexpected
DThe control was never dirty, so pristine is false
Step-by-Step Solution
Solution:
  1. Step 1: Understand pristine and dirty relationship

    Pristine means the control value has not changed since initialization; dirty means it has changed.
  2. Step 2: Analyze why pristine is false after markAsPristine()

    If the control value was changed after initialization, markAsPristine() resets the flag, but if value changed again later, pristine becomes false.
  3. Final Answer:

    The control value was changed after initialization -> Option B
  4. Quick Check:

    Value change after init makes pristine false [OK]
Quick Trick: Changing value after markAsPristine() resets pristine false [OK]
Common Mistakes:
MISTAKES
  • Assuming markAsPristine() always sets pristine true permanently
  • Ignoring value changes after calling markAsPristine()
  • Confusing pristine with touched

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes