Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cleanest way to reset forms

Tags:

forms

angular

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); } 
like image 590
Safal Pillai Avatar asked Jan 06 '17 06:01

Safal Pillai


People also ask

What is clean and preferred way to reset all objects in angular form?

In a model-driven form to reset the form we just need to call the function reset() on our myform model. The form now resets, all the input fields go back to their initial state and any valid , touched or dirty properties are also reset to their starting values.

How do I clear data from a form?

Form reset() Method The reset() method resets the values of all elements in a form (same as clicking the Reset button). Tip: Use the submit() method to submit the form.

How do you clear a reactive form?

In Reactive forms, we need to import FormGroup from '@angular/forms' . After importing the above-mentioned modules in the respective approach, angular forms module provides an inbuilt method called reset(). We can use the method and we can reset the form.


1 Answers

Add a reference to the ngForm directive in your html code and this gives you access to the form, so you can use .resetForm().

<form #myForm="ngForm" (ngSubmit)="addPost(); myForm.resetForm()"> ... </form> 

...Or pass the form to a function:

<form #myForm="ngForm" (ngSubmit)="addPost(myForm)"> ... </form> 
addPost(form: NgForm){     this.newPost = {         title: this.title,         body: this.body     }     this._postService.addPost(this.newPost);     form.resetForm(); // or form.reset(); } 

The difference between resetForm and reset is that the former will clear the form fields as well as any validation, while the later will only clear the fields. Use resetForm after the form is validated and submitted, otherwise use reset.


Adding another example for people who can't get the above to work.

With button press:

<form #heroForm="ngForm">     ...     <button type="button" class="btn btn-default" (click)="newHero(); heroForm.resetForm()">New Hero</button> </form> 

Same thing applies above, you can also choose to pass the form to the newHero function.

like image 54
smac89 Avatar answered Oct 23 '22 18:10

smac89