<form #myForm="ngForm" (submit)="addPost(); myForm.reset()"> ... </form>
Or pass the function form:
<form #myForm="ngForm" (submit)="addPost(myForm)"> ... </form>
addPost(form: NgForm){ this.newPost = { title: this.title, body: this.body } this._postService.addPost(this.newPost); form.reset(); // or form.resetForm(); }
We add one more example for people who cannot make the above work.
At the push of a button:
<form #heroForm="ngForm"> ... <button type="button" class="btn btn-default" (click)="newHero(); heroForm.reset()">New Hero</button> </form>
The same applies above, you can also pass the form to the newHero
function.
source share