0

Component Template

<form #formGroupContactUs_Template = "ngForm" (ngSubmit)="contactUsTemplateSubmit()">        
       <input type="text" class="form-control" name="name" [(ngModel)]="model.name">
       <!-- {{name!.value}} -->
       <button class="btn btn-primary mr-2" type="submit" 
       [disabled]="this.formGroupContactUs_Template.status != 'VALID'">Submit</button>
       <a class="btn btn-secondary" (click)="clearAll()">Clear All</a>
    </form>

Component Class

export class TemplateComponent implements OnInit {
 
  constructor() { }

  ngOnInit(): void {
  }

  model: any = {
    name: "",
  };

  contactUsTemplateSubmit() {
    console.log(this.model); //form Values
  }

  clearAll() {
    this.model.reset(); // not working
  }

}
1

2 Answers 2

2

OK, here is solution for clearing the form

in .ts

 clearAll(InputFormValue: ngForm) {
   InputFormValue.form.reset();//this will work
  }

in .html

<form #formGroupContactUs_Template="ngForm" (ngSubmit)="contactUsTemplateSubmit(formGroupContactUs_Template)">        
       <input type="text" class="form-control" name="name" [(ngModel)]="model.name">
       <!-- {{name!.value}} -->
       <button class="btn btn-primary mr-2" type="submit" 
       [disabled]="this.formGroupContactUs_Template.status != 'VALID'">Submit</button>
       <a class="btn btn-secondary" (click)="clearAll(formGroupContactUs_Template)">Clear All</a>
    </form>
Sign up to request clarification or add additional context in comments.

3 Comments

<a class="btn btn-secondary" (click)="clearAll()">Clear All</a> clearAll() have an error in templae: Expected 1 arguments, but got 0.ngtsc(2554)
i update the code, need to pass the parms to .ts
working now with changes
2

For programmatic approach you could get the form in ngAfterViewInit() and call reset on it. https://stackoverflow.com/a/52167560/15439733

Or on template this would work too.

   <a class="btn btn-secondary" (click)="formGroupContactUs_Template.reset()">Clear All</a>

Working example: https://stackblitz.com/edit/angular-workbench-template-driven-form-btfxnf?file=src%2Fapp%2Fapp.component.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.