我正在尝试验证以下字段。但是没有出现错误信息。请检查我的代码。谢谢
<div class="form-group">
<input type="password" minlength="10" required [ngClass]="{'is-invalid':lgform.submitted && password.invalid}" name="password" id="inputPassword" #password="ngModel" class="form-control" placeholder="Password" [(ngModel)]="model.password">
<div *ngIf="lgform.submitted && password.invalid" class="invalid-feedback">
<div *ngIf="password.errors['required']">Password is required</div>
<div *ngIf="password.errors['minlength']">minlength 10</div>
</div>
</div>
答案 0 :(得分:1)
在代码中使用<form #lgform="ngForm">
。
component.html
<form #lgform="ngForm">
<div class="form-group">
<input type="password" minlength="10" required [ngClass]="{'is-invalid':lgform.submitted && password.invalid}"
name="password" id="inputPassword" #password="ngModel" class="form-control" placeholder="Password"
[(ngModel)]="model.password">
<div *ngIf="lgform.submitted && password.invalid" class="invalid-feedback">
<div *ngIf="password.errors['required']">Password is required</div>
<div *ngIf="password.errors['minlength']">minlength 10</div>
</div>
</div>
<button type="submit" class="btn btn-success">Submit</button>
</form>
答案 1 :(得分:0)
看看我的图书馆Easy Angular。
https://github.com/adriandavidbrand/ezng
并查看StackBlitz演示,了解如何轻松进行表单验证。
https://stackblitz.com/edit/angular-8brst8?file=src%2Fapp%2Fapp.component.html
不需要任何样板代码。
<ez-password minlength="10" required>Password</ez-password>
是您所需要的全部。