我正在创建一个基本的示例用户注册应用程序,其中包含基本凭据。我正在尝试在单击取消按钮时重置表单。但是我无法这样做。我写过重置功能,但不知怎的,它没有被调用。我无法弄清楚是什么问题。请帮忙。谢谢你提前
我的代码:
ngOnInit()
{
this.addForm = this.formBuilder.group({
id: [],
userName: ['', Validators.required],
password:new FormControl( '',[ Validators.required]),
passwordAgain: new FormControl('',[ Validators.required]),
userRole:['',Validators.required],
},{ validator: RepeatPasswordValidator });
}
onSubmit() {
if (this.addForm.valid)
{
this.userService.createUser(this.addForm.value)
.subscribe( data => {
//this.router.navigate(['adduser']);
});
this.snackBar.open("message", "action", {
duration: 2000,
});
alert("User created");
}
window.location.reload();
}
changeClient(value) {
}
resetForm()
{
this.addForm.reset();
}
这是我的模板:
<div style="height: 100vh" fxLayout="column" fxLayoutAlign="center center" >
<mat-toolbar>
<span>Registration</span>
</mat-toolbar>
<mat-card>
<mat-card-content>
<form [formGroup]="addForm" class="login-form" (ngSubmit)="onSubmit()">
<div class="form-group">
<mat-form-field class="example-full-width">
<input matInput type="text" formControlName="userName" placeholder="userName" name="userName" class="form-control" id="userName">
</mat-form-field>
</div>
<div class="form-group">
<mat-form-field class="example-full-width">
<input matInput type="password" formControlName="password" placeholder="Password" name="password" class="form-control" id="password">
<mat-error *ngIf="addForm.controls.password.hasError('required')" >Passwords can't be empty</mat-error>
</mat-form-field>
</div>
<div class="form-field">
<mat-form-field>
<input matInput formControlName="passwordAgain" placeholder="Confirm the password" type="password" [errorStateMatcher]="passwordsMatcher">
<mat-error *ngIf="addForm.hasError('passwordsNotEqual')" >Passwords are different. They should be equal!</mat-error>
</mat-form-field>
</div>
<mat-form-field>
<mat-select placeholder="Roles" formControlName="userRole" id="userRole" (selectionChange)="changeClient($event.value)" [(value)]="selected">
<mat-option value="Admin">Admin</mat-option>
</mat-select>
</mat-form-field>
<div class="container" style="margin: 12px" fxLayout="row" fxLayoutAlign="center center">
<button mat-raised-button color="primary">Create</button>
<button mat-raised-button (click)= 'resetForm()' color="primary">Cancel</button>
</div>
</form>
</mat-card-content>
</mat-card>
</div>
它应该与resetForm()方法中的括号()
一样,如下所示:
this.addForm.reset();
更新的答案:
另外,尝试给type=reset
取消按钮,如下所示
<button mat-raised-button type=reset (click)= 'resetForm()' color="primary">Cancel</button>
重置应该是一个功能
resetForm()
{
this.addForm.reset();
}
如果你打电话给.reset();而不是.reset;
见here
如果这不是你的状态,请在你的函数中尝试一个console.log('调用reset'),如果调用它你也可以使用patchvalue()进行测试。 (重置应该工作)
编辑:你也可以从文档中试试这个:
console.log(this.addForm.value); // {password: 'abcdef', passwordAgain: 'abcdef'}
this.addForm.reset({ password: '', passwordAgain: '' });
如果这没有显示,我想看看你如何定义你的addForm
该按钮是一个提交按钮(这是所有浏览器的默认设置,Internet Explorer除外),请将类型设置为button
<button mat-raised-button type="button" (click)= 'resetForm()' color="primary">Cancel</button>