问题
I tried to use min validation in template form, but it didn't work. How to use it in template form? Thanks for your help.
<input type="number" class="form-control" name="srvc_sub_cnt{{i}}"
[ngModel]="s?.srvc_sub_cnt" (ngModelChange)="s?.srvc_sub_cnt?
s.srvc_sub_cnt=$event:null"
required #srvc_sub_cnt="ngModel" pattern="^[0-9]+$" min="1">
<div class="cell table-info" *ngIf="srvc_sub_cnt.errors?.min" class="form_error">
Service Subscribers must be greater than 0.
</div>
回答1:
To use min/max validations
on input of type number
you'll have to create Custom Validators
You can use this library. It implements a lot of custom validator
Including above library in your code, you can use min/max
like
<input type="number" [(ngModel)]="model.field" name="field" #field="ngModel" [min]="10"/>
<p *ngIf="field.errors?.min">error message</p>
来源:https://stackoverflow.com/questions/46961901/how-to-use-min-max-validation-in-template-form-angular-2