Within my template, there is a form that becomes visible when a button is clicked-
<form [formGroup]="person"
(ngSubmit)="onSubmitNewPerson()"
#formDirective="ngForm">
<mat-form-field>
<input matInput formControlName="firstName" required>
</mat-form-field>
<mat-form-field>
<input matInput formControlName="lastName" #last required>
</mat-form-field>
</form>
My component contains the following code-
@ViewChild('formDirective') formDirective: FormGroupDirective;
this.person = this.formBuilder.group({
lastName: ['', Validators.required],
firstName: ['', Validators.required]
});
Upon closing the form, I execute this function-
this.formDirective.resetForm();//hack that I saw in some question
this.person.reset();
However, when I reopen the form, I notice an immediate red error under the input field.
I have also attempted the following-
this.person.get('firstName').markAsUntouched();
this.person.get('lastName').markAsUntouched();
this.person.get('firstName').markAsPristine();
this.person.get('lastName').markAsPristine();
Unfortunately, this solution did not work either.
Does anyone have any ideas on how to resolve this issue?