I have a form that submits via an api and can be reused immediately after the post request has finished.
For validation styling requirements I need to remove the css class ng-submitted
from the <form>
I can't seem to find an angular method for clearing it, or resetting the form.
I have tried:
myForm.$submitted = false;
but this does not remove the ng-submitted
class
Does such a method exist in angular 1.3?
import { FormsModule } from '@angular/forms'; In Reactive forms, we need to import FormGroup from '@angular/forms' . After importing the above-mentioned modules in the respective approach, angular forms module provides an inbuilt method called reset(). We can use the method and we can reset the form.
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.
ng-pristine The field has not been modified yet. ng-dirty The field has been modified. ng-valid The field content is valid. ng-invalid The field content is not valid.
The ng-submit directive specifies a function to run when the form is submitted. If the form does not have an action ng-submit will prevent the form from being submitted.
hi I think you need to do this:
myForm.$setPristine();
myForm.$setUntouched();
this will clear the submit state and all fields will be untouched, this does not clear the field values though so if you want that you need to do this separately
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