As there is already an accepted answer, I can suggest a less added lines there.
That is a bit-known bug of Material, as a workaround you can do add an ElementReference to your form tag #f="ngForm":
<form [formGroup]="quickFileForm" (ngSubmit)="saveQuickFileForm()" #f="ngForm">
Then add a @ViewChild decorator to your TS part that refer to the tag in your form:
import { Component, ViewChild } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
group = new FormGroup({
select: new FormControl(null, Validators.required),
input: new FormControl()
});
@ViewChild('f') myForm;
....
....
}
Do not forget to import it above from @angular/core. Then merely add:
this.myForm.resetForm();
to your method:
submit() {
if(this.group.valid) {
const obj = new MyObj();
Object.assign(obj, this.group.value);
this.survey.push(obj);
this.myForm.resetForm(); // <-- Here you add it.
return;
}
alert('not valid, try again')
}
when submitting, the first field is empty & mark as error? - SeleM