我对一个需要验证的表单有这个问题,我正在下面尝试:
<div class="question">
<div class="row">
<h5>1. Requestors Name (Your name or JHED ID)</h5><p class="required">*</p>
</div>
<input type="text" class="form-control" id="requestorName" name="requestorName" required minlength="2"
[(ngModel)]="model.requestorName" #requestorName="ngModel"/>
</div>
<div *ngIf="requestorName.invalid && (requestorName.dirty || requestorName.touched)"
class="alert">
<div *ngIf="requestorName.errors?.['required']">
Requester name is required.
</div>
<div *ngIf="requestorName.errors?.['minlength']">
Requester name must be at least 2 characters long.
</div>
</div>
我使用了以下示例:https://angular.io/guide/form-validation
然而,当我尝试加载页面时,我会收到以下错误:
NG5002: Parser Error: Expected identifier for property access at the end of the expression [requesterName.errors?.['minlength']]
如果我删除最小长度div,我仍然会得到相同的精确错误,但对于表达式[requesterName.errors?.['required']]
我在这里错过了什么或做错了什么?在这方面有很多类似的问题,但它们都有类似的解决方案;使用CCD_ 2";如这个问题所示:角度验证错误:Property Required和Parser error,但我已经在做了。
我的有什么不同?
尝试requesterName?.errors?.required
您使用了两个不同的变量:
requesterName和requestorName
<input type="text" class="form-control" id="requestorName" name="requestorName" required minlength="2"
[(ngModel)]="model.requestorName" #requesterName="ngModel"/>