组件离开角度7后如何重置表格

时间:2019-11-06 04:51:29

标签: angular7 angular-reactive-forms

我正在处理角度为7的反应式表单。我有3个组件,每个组件都有一个表单。基于if,components开关。我不使用路线,只是隐藏并显示组件。如果我们返回到上一个组件。然后角材料输入有一个红色边框。如何删除该边框

dashboard.component.ts(父级)

<section>
  <mat-card class="auth-card">

    <mat-card-content>
      <div [ngStyle]="componentConst.showAcc ? {'display':'block'} : {'display':'none'}" id="acc ">
        <app-account (showStep)="onNextStepShow($event)"></app-account>
      </div>
      <div [ngStyle]="componentConst.showC ? {'display':'block'} : {'display':'none'}" id="cred ">
        <app-cred (showNextStep)="onNext($event)" (showStip)="onShowPrev()"></app-cred>
      </div>

appaccount.ts


    <form [formGroup]="providerForm" (ngSubmit)="onSubmit()">
      <div class="form-control">
        <mat-form-field>
          <input  type="text" placeholder="Provider" matInput [matAutocomplete]="auto"
            formControlName="providerName">
          <mat-autocomplete #auto="matAutocomplete" (optionSelected)="onSelectionChanged($event)">
            <mat-option *ngFor="let providerItem of providersData" [value]="providerItem.providerName">
              {{providerItem.providerName}}</mat-option>
            <mat-option *ngIf="providersData.length===0  && isSearching!='false'" disabled>Begin typing to search...
            </mat-option>
          </mat-autocomplete>
          <mat-error *ngIf="submitted && formControl.providerName.errors">
            <mat-error *ngIf="formControl.providerName.errors.required"> * Provider is required</mat-error>
          </mat-error>
        </mat-form-field>
      </div>
      <div class="button-section">
        <button mat-button [disabled]="!isSelected">Next</button>
      </div>
    </form>

1 个答案:

答案 0 :(得分:0)

在“切换时间”中,您应该重置表格。因此在那一次重置但触摸表单字段时,由于验证,您将得到一个红色边框。将此代码添加到表单事件之间的切换中。

this.providerForm.reset()
providerForm.markAsPristine();
providerForm.markAsUntouched();