Kun*_*nal 60 typescript angular
我正在使用Angular2模板来创建表单.
当我点击按钮时,页面刷新.
我的验证工作正常.
当用户点击按钮时,如何停止页面刷新?
以下是我正在使用的HTML: -
<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>
Run Code Online (Sandbox Code Playgroud)
red*_*ria 77
它刷新,因为在一个错误onSubmit处理程序..用event.PreventDefault();在onSubmit:
<form (ngSubmit)="onSubmit(empForm, $event)" ... >
public onSubmit(empForm: any, event: Event) {
event.preventDefault();
.... rest of your code
}
Run Code Online (Sandbox Code Playgroud)
您也可以检查控制台输出以调试错误...确保preserve log选中该选项
Ham*_*our 62
确保从包含组件的模块中的@ angular/forms导入FormsModule,因为如果没有它,您提交的表单将不断刷新页面并在不向控制台记录任何内容的情况下以静默方式失败.
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{ }
Run Code Online (Sandbox Code Playgroud)
Gün*_*uer 13
改为使用:
<button type="button"
Run Code Online (Sandbox Code Playgroud)
重新加载是由浏览器的默认提交行为引起的.
2019/2018 更新 - 如果您在任何最新版本的 Angular 上发生这种情况(我目前使用的是 7),这不是由于<button type="submit"...>,事实上,那完全没问题,您可以保留它。您还可以将(submit)事件保留在您的<form>元素上。
您可能在其他地方出现错误,导致 Angular 无法按预期运行。
FormsModule或。ReactiveFormsModuleConsole)如果您已正确实例化表单,Angular 将不会刷新页面。
小智 5
这有效并阻止了整个页面的重新加载:
我submitValues()在按钮道具中引入了一个 (click)= ,如下所示:
提交
这阻止了页面重新加载,我希望这会有所帮助。