模板驱动形式的最小长度误差,角度为8

问题描述 投票:0回答:1

单击提交按钮时出现错误提示。

ERROR TypeError: Cannot read property 'minlength' of null

我不知道为什么会收到此错误。因此如何解决此问题。

app.component.html:

<form name="form" (ngSubmit)="f.form.valid && onSubmit()" #f="ngForm" novalidate>
<div class="form-group">
                <label for="categoryName">Category Name:</label>
                <input type="text" class="form-control" name="categoryName" [(ngModel)]="cate.categoryName" minlength="5" #categoryname="ngModel" [ngClass]="{ 'is-invalid': f.submitted && categoryName.invalid }" required/>
                <div *ngIf="f.submitted && categoryName.invalid" class="invalid-feedback">
                    <div *ngIf="categoryName.errors.required">
                        Category Name is required
                    </div>
                </div>
                <div *ngIf="categoryName?.touched && categoryName?.errors.minlength" class="invalid-feedback">
                    Category Name must be at least 5 characters long.
                </div>
            </div>
</form>
typescript angular8
1个回答
0
投票

由于您删除了错误的根源,所以我假设错误来自于

此行:

<div *ngIf="categoryName?.touched && categoryName?.errors.minlength" class="invalid-feedback">

此处categoryName?.errors为空,并且您尝试使用此空值访问minlength

为了避免此错误,请在?之前添加.minlength运算符,例如:

<div *ngIf="categoryName?.touched && categoryName?.errors?.minlength" class="invalid-feedback">
© www.soinside.com 2019 - 2024. All rights reserved.