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

Angular MAT:窗体验证导致“jit\u nodeValue\u 6(…).hasError()不是函数”

  •  0
  • burnt1ce  · 技术社区  · 7 年前

    ERROR TypeError: jit_nodeValue_6(...).hasError is not a function

    title.hasError() . 有人知道为什么吗?谢谢!

    这是我的HTML模板:

    <form [formGroup]="detailsForm">
        <mat-form-field >        
            <input matInput placeholder="Title" formControlName="title" name="title" #title>
            <mat-error *ngIf="title.hasError('required')">
                    Title is <strong>required</strong>
            </mat-error>         
        </mat-form-field>
        ...
    </form>
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Vikas RyanSand20    7 年前

    如果你想要支票的话 FormControl 对于错误,您可以考虑 FormControlDirective 窗体控件 作为 @Input: 例子

      <input name="name" [formControl]="name">
    

    窗体控件 作为 formGroup 随着 safe-navigation-operator 又名埃尔维斯接线员

     detailsForm?.controls?.title?.hasError('required') 
    

    修改后的代码

    <div class="example-container">
        <form [formGroup]="detailsForm">
            <mat-form-field >        
                <input matInput placeholder="title" formControlName="title" name="title" #title>
                <mat-error *ngIf="detailsForm?.controls?.title?.hasError('required')">
                      <p>Required</p>
                </mat-error>         
            </mat-form-field>
        </form>
        </div>
    

    stackblitz

        2
  •  1
  •   Ramin Ar    5 年前

    ngIf 具体如下:

    *ngIf="detailsForm.get('title').hasError('required')"
    

    所以你必须改变 ngIf公司 mat-error 标记如下:

    <form [formGroup]="detailsForm">
        <mat-form-field >        
            <input matInput placeholder="Title" formControlName="title" name="title" #title>
            <mat-error *ngIf="detailsForm.get('title').hasError('required')">
                    Title is <strong>required</strong>
            </mat-error>         
        </mat-form-field>
        ...
    </form>
    
        3
  •  0
  •   Hemant Pandey    5 年前

    <div class="example-container">
        <form [formGroup]="detailsForm">
            <mat-form-field >        
                <input matInput placeholder="title" formControlName="title" ngModel name="title" #title="ngModel">
                <mat-error *ngIf="title?.hasError('required')">
                      <p>Required</p>
                </mat-error>         
            </mat-form-field>
        </form>
        </div>
    
    推荐文章