@Component({ selector: 'my-app', styles: [`#size { position: absolute; visibility: hidden; height: auto; width: auto; white-space: nowrap; /* Thanks to Herb Caudill comment */ }`] template: ` <div id="size" #size>{{text}}</div> <input #inp [(ngModel)]="text" (ngModelChange)="width=size.clientWidth" [style.width.px]="width > 50 ? width +10 : 60"> `, })
Player Link
It uses a hidden <div> , which needs the same font style as the input element, and with each change, the input will be updated according to the size of the hidden <div> that contains the same text.
See Also Calculate Text Width Using JavaScript
source share