我试图简单地从 Angular 网站复制演练,但无法进行长度验证:
<input
style="display:inline-block;min-width:150px"
class="form-input"
id="phone"
required
minlength="4"
maxlength="24"
type="number"
name="phone"
[(ngModel)]="client.phone"
#phone="ngModel" />
<!-- required works, minlength and maxlength always false -->
{{ phone.hasError('required') }} {{ phone.hasError('minlength') }} {{ phone.hasError('maxlength') }}
<div *ngIf="phone.errors && (phone.dirty || phone.touched)"
class="alert alert-danger">
<div [hidden]="!phone.errors.required">
Name is required
</div>
<div [hidden]="!phone.errors.minlength">
Name must be at least 4 characters long.
</div>
<div [hidden]="!phone.errors.maxlength">
Name cannot be more than 24 characters long.
</div>
</div>
我必须遗漏一些简单的东西,但由于某种原因,required
验证会根据输入而改变,但无论我的输入有多长minlength
,两者maxlength
都始终如此。false