How do I mark an Angular 2 Control as dirty in my code?
When I do it like this:
control.dirty = true;
I get this error:
Cannot set property dirty of #<AbstractControl> which has only a getter
You should use the markAsDirty method, like this: control. markAsDirty(); This will also mark all direct ancestors as dirty to maintain the model.
To prevent the validator from displaying errors before the user has a chance to edit the form, you should check for either the dirty or touched states in a control. When the user changes the value in the watched field, the control is marked as "dirty"
In a model-driven form to reset the form we just need to call the function reset() on our myform model. The form now resets, all the input fields go back to their initial state and any valid , touched or dirty properties are also reset to their starting values.
You should use the markAsDirty
method, like this:
control.markAsDirty();
This will also mark all direct ancestors as dirty to maintain the model.
Docs link
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With