1

我正在使用 Angular6 和角材料步进器。共有三种流程,其中步进控制具有一种形式,而第二步进控制具有 2 个输入字段。当我输入 firstStepperCtrl 并导航到第二步进器并单击 prev 并进入显示验证的第二步进器时。

在不触摸表单的情况下,将显示验证,如果已触摸第二个表单字段,如何重置步进器并验证步进器。

这是标记:

    <md-horizontal-stepper [linear]="isLinear" #stepper>
        <md-step [stepControl]="firstFormGroup">
            <form [formGroup]="firstFormGroup">
                <ng-template mdStepLabel>Fill out your name</ng-template>
                <md-form-field>
                    <input mdInput placeholder="Last name, First name" formControlName="firstCtrl" required>
                </md-form-field>
                <div>
                    <button md-button mdStepperNext>Next</button>
                </div>
            </form>
        </md-step>
        <md-step [stepControl]="secondFormGroup">
            <form [formGroup]="secondFormGroup"  #formDirective="ngForm">
                <ng-template mdStepLabel>Fill out your address</ng-template>
                <md-form-field>
                    <input mdInput placeholder="Address" formControlName="secondCtrl" required>
                </md-form-field>
            <md-form-field>
                    <input mdInput placeholder="phoneNo" formControlName="secondCtrlPhoneNo" required>
                </md-form-field>
                <div>
                    <button md-button mdStepperPrevious>Back</button>
                    <button md-button mdStepperNext>Next</button>
          <button md-button (click)="resetWholeStepper(stepper)">Reset</button>
            <button md-button (click)="reset2Stepper()">Reset1</button>
                </div>
            </form>
        </md-step>
        <md-step>
            <ng-template mdStepLabel>Done</ng-template>
            You are now done.
            <div>
                <button md-button mdStepperPrevious>Back</button>
        <button md-button (click)="resetStepper(stepper)">Reset</button>

            </div>
        </md-step>
    </md-horizontal-stepper>

app.component.ts

export class AppComponent implements OnInit, AfterViewInit {
  private ngVersion: string = VERSION.full;

  isLinear = false;
  firstFormGroup: FormGroup;
  secondFormGroup: FormGroup;

  constructor(private _formBuilder: FormBuilder,) { }

  // Event fired when component initializes
  ngOnInit() {
    this.firstFormGroup = this._formBuilder.group({
      firstCtrl: ['', Validators.required]
    });
    this.secondFormGroup = this._formBuilder.group({
      secondCtrl: ['', Validators.required],
      secondCtrlPhoneNo:['',Validators.required]
    });
  }

  // Event fired after view is initialized
  ngAfterViewInit() {
  }

  resetWholeStepper(stepper: MdStepper){
    stepper.selectedIndex = 0;
  }
  reset2Stepper1(formDirective: FormGroupDirective){
    alert(2);
    formDirective.resetForm();
    // this.secondFormGroup.clearValidators();
   this.secondFormGroup.reset();
  }

[问题截屏链接]] 1

演示

4

0 回答 0