Angular Material Vertical Stepper 单一形式 .reset() 无法正常运行

标签 angular angular7 angular-material-stepper

我一直在为我的一个网页开发步进元素,并尝试实现 .reset() 功能,以便使用 Material 设计将表单恢复为空白状态文档 ( https://material.angular.io/components/stepper/overview )

按照多表单组示例,我可以看到 .reset 通过将您带回到第一步并清除表单来正常工作。但是,在尝试使用单一表单结构时,我发现 stepper.reset() 会将您带回到第一步,但不会清除表单。

我已经尝试将重置按钮标记为 Type="button",因为单个表单要求在 Next 和 Back 按钮上使用该按钮以防止在单击按钮时提交表单,但没有看到任何变化。

我希望在单击重置时执行这两个操作,但我不确定我是否必须编写手动重置函数,或者我的代码中是否存在错误。

如有任何帮助,我们将不胜感激。谢谢!

create.component.html

<form [formGroup]="formGroup" class="content-container" (ngSubmit)="onSubmit()">
  <h1>{{title}}</h1>
  <mat-vertical-stepper #stepper>
    <mat-step formGroupName="someFieldGroup" [stepControl]="someFieldGroup">
      <ng-template matStepLabel>Fill out Field</ng-template>
      <mat-form-field>
        <input matInput placeholder="Some Field" formControlName="someFieldCtrl" name="someFieldName" required>
        <mat-error>This field is required</mat-error>
      </mat-form-field>
      <div>
        <button type="button" mat-button matStepperNext>Next</button>
      </div>
    </mat-step>
    <mat-step formGroupName="anotherFieldGroup" [stepControl]="anotherFieldGroup">
      <ng-template matStepLabel>Fill out Field</ng-template>
      <mat-form-field>
        <input matInput placeholder="Another Field" formControlName="anotherFieldCtrl" name="anotherFieldName" required>
        <mat-error>This field is required</mat-error>
      </mat-form-field>
      <div>
        <button type="button" mat-button matStepperPrevious>Back</button>
        <button type="button" mat-button matStepperNext>Next</button>
      </div> 
    </mat-step>
    <mat-step formGroupName="dropdownFieldGroup" [stepControl]="dropdownFieldGroup">
      <ng-template matStepLabel>Select Option</ng-template>
      <mat-form-field>
        <mat-select placeholder="Select Option" disableOptionCentering formControlName="dropdownFieldCtrl" name="dropdownFieldName" required>
          <mat-option value="1">One</mat-option>
          <mat-option value="2">Two</mat-option>
          <mat-option value="3">Three</mat-option>
          <mat-option value="4">Four</mat-option>
        </mat-select>
        <mat-error>This field is required</mat-error>
      </mat-form-field>
      <div>
        <button type="button" mat-button matStepperPrevious>Back</button>
        <button type="button" mat-button matStepperNext>Next</button>
      </div>
    </mat-step>
    <mat-step>
      <ng-template matStepLabel>Done</ng-template>
      You are now done.
      <div>
        <button type="button" mat-button matStepperPrevious>Back</button>
        <button type="submit" mat-button>Done</button>
        <button mat-button (click)="stepper.reset()">Reset</button>
      </div>
    </mat-step>
  </mat-vertical-stepper>
</form>

创建.component.ts

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';

//Type Imports
import { Service } from '../service';
import { Post } from './post';

@Component({
  selector: 'app-create',
  templateUrl: './create.component.html',
  styleUrls: ['./create.component.css']
})
export class CreateComponent implements OnInit {

  title = 'Create';

  formGroup: FormGroup;
  post: Post = {} as Post;

  constructor(private service: Service) { }

  ngOnInit() {
    this.formGroup = new FormGroup({
      someFieldGroup: new FormGroup({
        someFieldCtrl: new FormControl(null)
      }),
      anotherFieldGroup: new FormGroup({
        anotherFieldName: new FormControl(null)
      }),
      dropdownFieldGroup: new FormGroup({
        dropdownFieldCtrl: new FormControl(null)
      })
    });
  }

  onSubmit() {
    this.post.someField = this.formGroup.get('someFieldGroup').get('someFieldName').value;
    this.post.anotherField = this.formGroup.get('anotherFieldGroup').get('anotherFieldName').value;
    this.post.dropdownField = this.formGroup.get('dropdownFieldGroup').get('dropdownFieldName').value;

    this.service.create(JSON.stringify(this.post)).subscribe(data => { console.log(data) });
  }
}

最佳答案

我也遇到了这个问题。在步进器上调用 reset 似乎只会让您回到第一步。如果您还对表单本身调用重置,则表单字段应该清除。

例子:

<form [formGroup]="formGroup" class="content-container" (ngSubmit)="onSubmit() 
#myFormGroup">
...
  <button mat-button (click)="myFormGroup.reset();stepper.reset()">Reset</button>
      </div>
    </mat-step>
  </mat-vertical-stepper>
</form>

关于Angular Material Vertical Stepper 单一形式 .reset() 无法正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53780841/

相关文章:

Angular Mat DatePicker 格式问题

angular - 如何从 Ionic 2 中导航到的页面访问主要组件功能?

angular - 单击时将焦点设置为 textarea angular 5

angular - 使用 ngx-bootstrap 从 angular 7 更新到 Angular 8.0.0-beta.7 时出错

javascript - MatStepper 如何触发步骤的错误状态?

angular - 如何设置material stepper默认打开第二个item并设置图标颜色?(Angular Material)

javascript - 使用 Angular Material 步进器在输入类型文本中添加自定义验证

javascript - 在我单击 UI 之前,signInWithPopup Promise 不会执行 .catch。 Angular 和 Firebase

angular - Angular 中递归对象(通过调用静态方法链接)中的循环依赖

如果相关表单组无效,Angular 7 和表单数组会禁用表单组的按钮,并给出未定义的错误