0
votes

I have Angular form (not reactive):

<form #form="ngForm" (submit)="submit(); form.reset()">
 <input ... />
</form>

But form does not reseting after submit, and inputs still marked as touched.

Please, explain, how can i reset form in Angular 5?

StackBlitz: https://stackblitz.com/edit/reset-form-demo-jdsdwe2

What i want: Clear focus and mark as untouched after submit.

1
Can you show this behavior on stackblitz? I ask it because it works for me - yurzui
Please provide an minimal reproducible example - Igor
@yurzui i add link in question - Vladimir Humeniuk
The field is cleared if I remove [ngModelOptions]="{standalone: true}" and give a name to the input element. See this stackblitz. - ConnorsFan
@ConnorsFan ye, its work. You are right as always. Thanks ) - Vladimir Humeniuk

1 Answers

1
votes

Using @angular/forms when you use a <form> tag it automatically creates a FormGroup.

For every contained ngModel tagged it will create a FormControl and add it into the FormGroup created above; this FormControl will be named into the FormGroup using attribute name.

Example:

<form #f="ngForm">
    <input type="text" [(ngModel)]="firstFieldVariable" name="firstField">
    <span>{{ f.controls['firstField']?.value }}</span>
</form>

When you mark it as standalone: true this will not happen (it will not be added to the FormGroup).