角2:长度验证工作不
问题描述:
我想简单地复制从角网站上的演练,但不能得到长度验证工作:角2:长度验证工作不
<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
。
答
它,因为数量型可是没有length属性
有这个https://github.com/angular/angular/issues/15053
你可以解决它的错误,如:
[ngModel]="client.phone" (ngModelChange)="client.phone = $event + ''"
更新
由于4.2.0-beta.0(2017年5月4日)介绍了最小值和最大值验证器,所以你可以看看https://github.com/angular/angular/pull/15813/commits/52b0ec8062381e7285b5f66aa83008edfbf02af3
我知道我失去了一些东西。谢谢! –
如果我不希望在提交时发生变化,该怎么办? –