<input oninput="this.value = this.value.toUpperCase()" type="text" id="roleName" name="roleName" class="form-control width200px" [(ngModel)]="role.roleName">
UI 受到影響并且大寫輸入,我只想將 role.roleName 以大寫形式發送到服務器,而無需在 UI 上對其進行轉換。
uj5u.com熱心網友回復:
NgModel 是雙向系結的,因此如果值被轉換,那么 UI 也會被反映。如果您希望有不同的值,您可以嘗試將值拆分為兩個引數,一個用于查看,另一個用于發送到服務器。
樣本:
<input oninput="role.roleName = this.value.toUpperCase()" type="text" id="roleName" name="roleName" class="form-control width200px" [(ngModel)]="role.somethingElse">
uj5u.com熱心網友回復:
然后您必須在將值放入請求物件之前將其設為大寫。
在打字稿中只需使用 .toUpperCase()
或者,您也可以在模型上使用帶有 setter 的 ngmodel 擴展格式。
.ts
class Role {
roleName: string;
getName(): string {
return this.roleName;
}
setName(value: string): void {
this.roleName = value.toUpperCase();
}
}
.html
<input
type="text"
id="roleName"
name="roleName"
class="form-control width200px"
[ngModel]="role.getName()"
(ngModelChange)="role.setName($event)"
/>
請查看 Stackblitz:https ://stackblitz.com/edit/angular-3u48kn
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/358485.html
上一篇:使立方體變小
