尝试在角度单击按钮时重置表单

时间:2019-04-11 11:06:25

标签: angular typescript angular-forms

我正在创建具有基本凭据的基本示例用户注册应用程序。我试图在单击“取消”按钮时重置表单,但是无法这样做。我已经编写了reset函数,但不知何故没有调用它。我不知道出了什么问题。请帮忙。谢谢

我的ts代码:

 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>

5 个答案:

答案 0 :(得分:2)

它应该在 resetForm()方法内的括号()中,如下所示:

this.addForm.reset();

更新后的答案:

此外,请尝试为type=reset赋予如下所示的取消按钮

<button mat-raised-button type=reset (click)= 'resetForm()' color="primary">Cancel</button>

答案 1 :(得分:0)

重置应为函数

resetForm()
{
  this.addForm.reset();
}

答案 2 :(得分:0)

如果您调用.reset(),可能会得到解决。而不是.reset;

请参见here

如果这在您的状态下不起作用,请尝试在您的函数中使用console.log(称为“ reset”),如果被调用,您也可以使用patchvalue()进行测试。 (虽然重置应该可以)

编辑:您也可以从文档中尝试以下操作:

console.log(this.addForm.value);  // {password: 'abcdef', passwordAgain: 'abcdef'}

this.addForm.reset({ password: '', passwordAgain: '' });

如果未显示,那么我想看看您如何定义addForm

答案 3 :(得分:0)

该按钮是一个提交按钮(这是所有浏览器的默认设置,Internet Explorer除外) , 请将类型设置为button

    <button mat-raised-button type="button" (click)= 'resetForm()' color="primary">Cancel</button>

答案 4 :(得分:0)

不需要新方法...
只需将按钮类型设置为reset,如下所示,

<button mat-button color="primary" type="reset">Cancel</button>

这应该工作!!