2017-04-26 108 views
0

我想簡單地複製從角網站上的演練,但不能得到長度驗證工作:角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驗證根據輸入而改變,但無論我的輸入有多長時間,minlengthmaxlength總是false

回答

4

它,因爲數量型可是沒有length屬性

enter image description here

有這個https://github.com/angular/angular/issues/15053

你可以解決它的錯誤,如:

[ngModel]="client.phone" (ngModelChange)="client.phone = $event + ''" 

Plunker Example

更新

由於4.2.0-beta.0(2017年5月4日)介紹了最小值和最大值驗證器,所以你可以看看https://github.com/angular/angular/pull/15813/commits/52b0ec8062381e7285b5f66aa83008edfbf02af3

+0

我知道我失去了一些東西。謝謝! –

+0

如果我不希望在提交時發生變化,該怎麼辦? –