【问题标题】:How can I reset Angular Reactive Form如何重置 Angular 反应式表单
【发布时间】:2018-07-22 19:01:58
【问题描述】:

我尝试过但未能找到重置我的角度形式的方法。

有人可以帮忙吗?

<form #thisIsAForm>
  <mat-form-field class="full-width">
    <input matInput placeholder="Weather">
  </mat-form-field>
</form>
<button mat-raised-button (click)="resetForm()">Reset</button>

export class Example{
  @ViewChild('thisIsAForm') thisIsAForm;

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

【问题讨论】:

    标签: angular angular-reactive-forms


    【解决方案1】:

    几乎!为此使用反应形式:

    <form [formGroup]="myForm">
      <mat-form-field class="full-width">
        <input matInput placeholder="Weather" formControlName="weather">
      </mat-form-field>
    </form>
    <button mat-raised-button (click)="myForm.reset()">Reset</button>
    
    
    export class Example{
      myForm: FormGroup;
    
      constructor(private fb: FormBuilder) { 
        this.myForm = fb.group({
          weather: ''
        });
      }
    
      // If the HTML code doesn't work, simply call this function
      reset() {
        this.myForm.reset();
      }
    }
    

    【讨论】:

    • 这在 stackblitz 中完美运行,但是当我在我的实际代码中使用它时,我得到了可怕的错误Can't bind to 'formGroup' since it isn't a known property of 'form'。我检查了所有内容,发现我已经正确导入了 FormsModule 和 ReactiveFormsModule ......我不明白出了什么问题。
    • 从头开始!我发现我是个白痴。我忘记了使用清除表单功能的组件本身是通过另一个导入文件导入到 app.module.ts 中的,所以我添加了 ReactiveFormsModule 作为中间文件的导入,一切都很好!
    【解决方案2】:
    <form [formGroup]="thisIsAForm" (ngSubmit)="onSubmit()">
      <mat-form-field class="full-width">
        <input formControlName="weather" placeholder="Weather">
      </mat-form-field>
    </form>
    <button mat-raised-button (click)="resetForm()">Reset</button>
    
    
    export class Example{
      thisIsAForm: FormGroup;
    
      constructor() {
        this.thisIsAForm = new FormGroup(
          weather: new FormControl('')
        ); 
      }
    
      resetForm() {
        this.thisIsAForm.reset();
      }
    }
    

    【讨论】:

    • Arg @trichetriche 更快:P
    【解决方案3】:

    在 Angular 8 中,如果你这样做了

    <form #form="ngForm" (ngSubmit)="process(form)">
    
    process(form: NgForm) { ...
    

    使用--prod构建时会出现以下错误

    “FormGroupDirective”类型的参数不可分配给参数 'NgForm' 类型。

    (ngSubmit)

    我所做的是将form 模板引用注入为FormGroupDirective,然后调用resetForm()

    <form #form (ngSubmit)="process(form)">
    
    @ViewChild('form', { static: false }) FormGroupDirective formDirective;
    

    FormGroupDirective

    【讨论】:

      【解决方案4】:

      我认为您需要在 html 代码中添加 ngForm 指令,如下所述 stackbliz 示例还将名称和 ngModel ngControl 添加到表单元素。

      Stackbliz: Template Driven Form Reset

      【讨论】:

        【解决方案5】:

        我在这里重置模板驱动的表单,而不会使表单变脏。

            <form class="example-form" #charreplaceform="ngForm">
                  <mat-form-field class="example-full-width">
                    <input matInput #codepointSel="ngModel" (change)="createReplacementChar()" (keyup)="checkForNumber()"
                      required [(ngModel)]="charReplaceInfo.codePoint" name="code-point" placeholder="Code Point (Only number)"
                      [disabled]="isDisabled">
                  </mat-form-field>
        
                  <a (click)="refreshCharRecord(charreplaceform)">
                    <i class="material-icons">
                      loop
                    </i>
                  </a>
            </form>
        
            // in .ts
            refreshCharRecord(form: NgForm) { // getting the form reference from template 
            form.form.reset();   // here we are resetting the form without making form dirty
          }
        

        【讨论】:

          【解决方案6】:

          对我来说,将 type="reset" 添加到重置按钮已解决问题

          <button **type = "reset"** mat-raised-button (click)="resetForm()">Reset</button>
          

          【讨论】:

            猜你喜欢
            • 2019-06-24
            • 1970-01-01
            • 2019-12-28
            • 2018-11-02
            • 2021-01-12
            • 1970-01-01
            • 2017-12-15
            • 1970-01-01
            • 2019-08-11
            相关资源
            最近更新 更多