Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ngSubmit refreshes the page in Angular 2 form

I am using Angular2 template for creating a form.

When i click on button, the pages refreshes.

My validations are working fine.

How can i stop page refresh when user clicks on button?

Following is HTML I am using:-

<div class="panel panel-default">     <div class="panel-heading">         <h3 class="panel-title">Add Employee</h3>         {{model | json}}         {{EName.errors | json}}     </div>     <div class="panel-body">           <form (ngSubmit)="onSubmit(empForm)" #empForm="ngForm" autocomplete="off" novalidate>          <div class="form-group">             <label for="EmployeeName">Employee Name</label>             <input type="text" class="form-control" id="EmployeeName" placeholder="Employee Name" required [(ngModel)]="model.EName" name="EName" #EName="ngModel" ngControl="Ename" #EName="EName" >             <div *ngIf="EName.touched && EName.errors" >                 <div *ngIf="EName.errors.required"  class="alert alert-danger">                     Employee Name is required                 </div>             </div>         </div>         <div class="form-group">             <label for="Age">Age</label>             <input type="text" class="form-control" id="Age" name="Age" placeholder="Age" [(ngModel)]="model.Age" ngControl="Age">         </div>         <div class="form-group">                         <label for="Sex">Sex</label>             <div class="d-block">                 <label class="radio-inline">                     <input type="radio" name="sex" id="Male" value="0" (click)="model.Sex=$event.target.value"> Male                 </label>                 <label class="radio-inline">                     <input type="radio" name="sex" id="Female" value="1" (click)="model.Sex=$event.target.value"> Female                 </label>             </div>         </div>          <div class="form-group">             <label for="DOJ">Date of Joining</label>             <datepicker [(ngModel)]="model.DOJ" name="DOJ"></datepicker>         </div>          <div class="form-group">             <label for="Salary">Salary</label>             <input type="text" class="form-control" id="Salary" placeholder="Salary" [(ngModel)]="model.Salary" name="Salary">         </div>          <div class="form-group">             <label for="Designation">Designation</label>             <select id="Designation" name="designation" class="form-control" required [(ngModel)]="model.Designation" name="designation" #desig="ngForm" ngControl="Designation">                 <option value="" selected>-- Select  --</option>                 <option *ngFor="let designation of designations" value="{{ designation.id }}">                      {{designation.name}}                  </option>             </select>             <div [hidden]="desig.valid || desig.pristine" class="alert alert-danger">                 Please select a proper designation.             </div>         </div>         <button type="submit" class="btn btn-default" [disabled] ="!empForm.form.valid">Submit</button>         </form>     </div> </div> 
like image 807
Kunal Avatar asked Aug 29 '16 09:08

Kunal


People also ask

How to use reset() in angular?

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.

What is Ngsubmit in angular?

The ng-submit directive specifies a function to run when the form is submitted. If the form does not have an action ng-submit will prevent the form from being submitted.

How to reset form in angular material?

Resetting a form in template-driven approach: In template driven approach, we need to import NgForm from '@angular/forms' and we use [(ngModel)] directive for two way data-binding and we should also import FormsModule from '@angular/forms' in app. module. ts file.

How to reset a form field in angular?

How to clear an input field in Angular Forms. In a normal HTML form you would use find the input on the page and just clear it's text value. In Angular you want to bind the value of a property to the input box and reset the properties value.


2 Answers

Make sure you import FormsModule from @angular/forms in the module containing your component because without it your form on submit will keep refreshing the page and failing silently without logging anything to the console.

import { NgModule }           from '@angular/core'; import { CommonModule }       from '@angular/common';  /*make sure you import it here*/ import { FormsModule }        from '@angular/forms';   @NgModule({   /*and add it to the imports array here*/   imports:      [ FormsModule,  CommonModule],   declarations: [ YourFormComponent ],   exports:      [],   providers:    [], })  export class FeatureModule{ } 
like image 84
Hamed Baatour Avatar answered Oct 01 '22 21:10

Hamed Baatour


it refreshes because there is an error in onSubmit handler.. use event.PreventDefault(); in the onSubmit :

 <form (ngSubmit)="onSubmit(empForm, $event)" ... >   public onSubmit(empForm: any, event: Event) {   event.preventDefault();   .... rest of your code } 

also you can just check the console output to debug the error ... make sure to mark the preserve log option

preserve log checked in devtools

like image 20
reda igbaria Avatar answered Oct 01 '22 22:10

reda igbaria