In Angular Reactive Forms, MarkAsTouched and MarkAsDirty are the methods that control the state of a form control. They represent two different aspects of the control's status. A developer needs to remember both of them when deciding when to use each one.
Definition: This method marks a form control as "touched."
When it’s used: It is used to indicate that the user has interacted with the form control (ex: clicked or focused and then blurred).
Purpose: Marking a form control as touched is useful to trigger visual feedback, such as showing error messages only after the user has interacted with the input. This can help improve user experience by not showing errors prematurely.
Example:
controlSelf.markAsTouched();
Definition: This method marks a form control as "dirty."
When it’s used: It is used to indicate that value of the form control has been modified (ex: user has changed the value from its initial value).
Purpose: It helps to track if the value of the form control has been modified and is useful for features that allow enabling or disabling save buttons or form submission based on if changes have been made.
Example:
controlSelf.markAsDirty();
Touched: Relates to whether the user has interacted with the control (focused and then blurred).
Dirty: Relates to whether the value of the control has changed from its initial value.
Touched might be used to display error messages only after the user has tried to interact with the field.
Dirty might be used to determine if there’s any unsaved data, so you can show a prompt or a save button.
Ready to transform your business with our technology solutions? Contact Us today to Leverage Our Angular Expertise.
0