我正在做一個角度專案,我想創建一個只接受 3 個值 0、0.5、1 的輸入,如果用戶輸入例如 0.4,他將收到一條錯誤訊息并告訴他只輸入 3 個值中的一個值{ 0 /0.5 / 1}。
uj5u.com熱心網友回復:
您可以將 Angular模板驅動表單與模式驗證器一起使用,如下所示:
app.component.ts
export class AppComponent {
model: any = {};
}
app.component.html
<input
type="number"
name="quantity"
[(ngModel)]="model.quantity"
#quantity="ngModel"
pattern="^(0|0.5|1)$"
required
/>
<div *ngIf="quantity.invalid && (quantity?.dirty || quantity?.touched)">
Quantity is invalid! Only allowed values are: 0, 0.5, 1
</div>
Stackblitz 試穿:鏈接
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/513409.html
上一篇:Pydantic與列舉子類?
