我想通过 Angular 6 中的输入类型编号保存长度。但长度不应为 0 或小于 0 。当有人输入 0 或小于输入值时,它应该在按钮单击时显示错误。我尝试添加 min="1"但它不起作用
<mat-form-field class="example-full-width">
<input matInput type="number" [(ngModel)]="ItemModel.Length" placeholder="Length" min="1"
id="Length" name="Length" required>
</mat-form-field>
最佳答案
您可以将最小值设置为 0.1 并为红色边框添加 ngclass
<mat-form-field class="example-full-width">
<input matInput [(ngModel)]="ItemModel.Length" type="number" name="Length"
placeholder="Length" [ngClass]="{ 'red': ItemModel.Length <= 0 &&
greaterthenzero }" min="0.1" required>
</mat-form-field>
在提交按钮上,您可以检查您的模型值并添加一个 bool 属性,如果值小于 0 则设置为 true
submit() {
this.greaterthenzero = false;
if (this.ItemModel.Length <= 0) {
this.greaterthenzero = true;
}
}
关于javascript - 如何在单击按钮时验证输入不保存 0 或小于 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58235309/