I have created a function in one of my component file that resets the form(myform):
`onSubmit() {
if (this.myform.valid) {
console.log("Form Submitted!");
this.myform.reset();
}
}`
It works perfectly fine resetting the whole form, but is it possible to just reset some of the elements and keeping other the same way.
To reset a form control, right-click the control in the form, and then select the Reset option from the context menu.
The reset() method resets the values of all elements in a form (same as clicking the Reset button). Tip: Use the submit() method to submit the form.
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.
Calling the reset function on a form model resets the form back to its original pristine state.
try this:
this.myform.controls['comments'].reset()
try this one:
clearForm() {
this.myForm.get('comments').reset();
this.myForm.get('name').reset();
}
and call this function where you submit form.
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