You can split [(ngModel)]
into [ngModel]
and (ngModelChange)
, and convert the input text to uppercase in the event handler:
<input [ngModel]="value" (ngModelChange)="value = $event.toUpperCase()" />
However, there is a problem when a lowercase letter is inserted at the beginning or in the middle of the input text: the caret automatically moves to the end of the text. The technique suggested in this article can be used to maintain the caret position, as shown in the following improved code:
<input #txt [ngModel]="value" (ngModelChange)="value = convertToUpperCase(txt, $event)" />
public convertToUpperCase(input: HTMLInputElement, val: string) {
// Save caret position
const start = input.selectionStart;
const end = input.selectionEnd;
setTimeout(() => {
// Restore caret position after returning the converted string
input.setSelectionRange(start, end);
});
// Return the input string converted to uppercase
return val.toUpperCase();
}
See this stackblitz for a demo.