本文介绍了ngSubmit以Angular 2形式刷新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Angular2模板创建表单.
I am using Angular2 template for creating a form.
当我单击按钮时,页面会刷新.
When i click on button, the pages refreshes.
我的验证工作正常.
当用户单击按钮时,如何停止页面刷新?
How can i stop page refresh when user clicks on button?
以下是我正在使用的HTML:-
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>
推荐答案
它会刷新,因为onSubmit
处理程序中存在错误.在onSubmit
中使用event.PreventDefault();
:
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
}
您也可以只检查控制台输出以调试错误...确保标记preserve log
选项
also you can just check the console output to debug the error... make sure to mark the preserve log
option
这篇关于ngSubmit以Angular 2形式刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!