據thoughtgram.io,目前支持的驗證程序是:最小/最大驗證在角2最終
- 需要
- MINLENGTH個
- 最大長度
- 模式
因此,考慮到以下代碼(plunkr here):
@Component({
selector: 'my-app',
template: `
<form #formRef="ngForm">
<input type="number" [(ngModel)]="firstValue" name="firstValue" min="0" required/>
<input type="text" [(ngModel)]="secondValue" maxlength="5" name="secondValue" required/>
<button type="submit"> Submit </button>
</form>
FORM: {{formRef.form | json }}
`
})
export class AppComponent {
firstValue = -22;
secondValue = "eyy macarena!";
}
雖然minlength
支持,min="0"
由角驗證忽略:
因此,爲了使窗體導致錯誤時firstValue ngModel < 0,我需要建立一個自定義驗證器?
'FirstValue <0',和' David
是的,你需要。在這裏檢查源代碼:https://github.com/angular/angular/blob/master/modules/%40angular/forms/src/directives/validators.ts只有那些由thetram文章提到的4個驗證器才支持OOTB。 –