What is the cleanest way to reset forms in Angular 2 latest version? I would like to reset the input textboxes after adding a post.
@Component({
selector: 'post-div',
template: `
<h2>Posts</h2>
<form (submit)="addPost()">
<label>Title: </label><input type="text" name="title" [(ngModel)]="title"><br/>
<label>Body: </label><input type="text" name="body" [(ngModel)]="body"><br/>
<input type="submit" value="Add Post">
</form>
<ul>
<li *ngFor="let post of posts">
<strong>{{post.title}}</strong>
<p>{{post.body}}</p>
</li>
</ul>
`,
providers: [PostService]
});
addPost(){
this.newPost = {
title: this.title,
body: this.body
}
this._postService.addPost(this.newPost);
}
easiest way to clear form
<form #myForm="ngForm" (submit)="addPost();"> ... </form>
then in .ts file you need to access local variable of template i.e
@ViewChild('myForm') mytemplateForm : ngForm; //import { NgForm } from '@angular/forms';
for resetting values and state(pristine,touched..) do the following
addPost(){
this.newPost = {
title: this.mytemplateForm.value.title,
body: this.mytemplateForm.value.body
}
this._postService.addPost(this.newPost);
this.mytemplateForm.reset(); }
This is most cleanest way to clear the form
Doing this with simple HTML and javascript by casting the HTML element so as to avoid typescript errors
<form id="Login">
and in the component.ts file,
clearForm(){
(<HTMLFormElement>document.getElementById("Login")).reset();
}
the method clearForm()
can be called anywhere as need be.
Easiest and cleanest way to clear forms as well as their error states (dirty, pristine etc)
this.formName.reset();
for more info on forms read out here
PS: As you asked a question there is no form used in your question code you are using simple two-day data binding using ngModel, not with formControl
.
form.reset()
method works only for formControls reset call
The simplest method to clear a form with a button in angular2+ is
give your form a name using #
<form #your_form_name (ngSubmit)="submitData()"> </form>
<button (click)="clearForm(Your_form_name)"> clear form </button>
in your component.ts file
clearForm(form: FormGroup) {
form.reset();
}
To reset the form call the reset function with the form name in the same structure as like from group
addPost(){
this.newPost = {
title: this.title,
body: this.body
}
this.formName.reset({
"title": '',
"body": ''
});
}
component.html (What you named you form)
<form [formGroup]="contactForm">
(add click event (click)="clearForm())
<button (click)="onSubmit()" (click)="clearForm()" type="submit" class="btn waves-light" mdbWavesEffect>Send<i class="fa fa-paper-plane-o ml-1"></i></button>
component.ts
clearForm() {
this.contactForm.reset();
}
view all code: https://ewebdesigns.com.au/angular-6-contact-form/ How to add a contact form with firebase
For just to reset the form use reset()
method. It resets the form but you could get issue such as
validation errors - ex: Name is required
To solve this use resetForm()
method. It resets the form and also resets the submit status solving your issue.
The resetForm()
method is actually calling reset() method and additionally it is resetting the submit status.
//Declare the jquery param on top after import
declare var $: any;
declare var jQuery: any;
clearForm() {
$('#contactForm')[0].reset();
}
this.<your_form>.form.markAsPristine();
this.<your_form>.form.markAsUntouched();
this.<your_form>.form.updateValueAndValidity();
can also help
You can also do it through the JavaScript:
(document.querySelector("form_selector") as HTMLFormElement).reset();
Angular Reactive Forms:
onCancel(): void {
this.registrationForm.reset();
this.registrationForm.controls['name'].setErrors(null);
this.registrationForm.controls['email'].setErrors(null);
}
Source: Stackoverflow.com