代码之家  ›  专栏  ›  技术社区  ›  ackuser

在角度5手动设置值后验证反应形式?

  •  2
  • ackuser  · 技术社区  · 6 年前

    它们是更新的,尽管它们没有通过任何形式的验证。

    就像是

    this.FORM.controls.WHATEVERINPUT.setValue(X);
    

    <mat-form-field>
    <input matInput formControlName="WHATEVERINPUT">
    <mat-error *ngIf="FORM?.controls.WHATEVERINPUT.errors">
     ... is required & Minimum of 5 characters
    </mat-error>
    </mat-form-field>
    

    除了我之前提到的那些价值观之外,它完全可以工作。

      this.FORM.controls.WHATEVERINPUT.markAsTouched();
        this.FORM.controls.WHATEVERINPUT.markAsDirty();
        this.FORM.updateValueAndValidity();
    

    在很多方面,但我还没有得到 已验证

    我很感激有人能以任何方式帮助我 谢谢

    1 回复  |  直到 6 年前
        1
  •  0
  •   John    6 年前

    编辑:简单回答

    你可以打电话 this.form.get('username').markAsTouched();

    另一个答案

    如果将按钮放在表单中,则添加 (ngSubmit)="USR($event)" type="submit" 在您的按钮上,您将进行角度检测,如果形式是有效的或不是,然后将显示错误消息。

    <form class="example-form" [formGroup]="form" (ngSubmit)=USR($event)>
      <mat-form-field class="example-full-width">
        <input matInput placeholder="Username" formControlName="username">
      </mat-form-field>
    
      <mat-form-field class="example-full-width">
        <input matInput placeholder="Password" formControlName="password">
      </mat-form-field>
    
      <button mat-raised-button type="button" color="primary" [style.margin]="'12px'" (click)="PWD()">Update password with value</button>
      <button type="submit" mat-raised-button color="primary" [style.margin]="'12px'">Update username required error</button>
    </form>
    

    Here is a working example