Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An Angular directive that adjust input width based on the content size.
import { Directive, ElementRef, HostListener } from '@angular/core';
@Directive({
selector: '[dynamic-input]',
})
export class DynamicInputDirective {
constructor(private el: ElementRef) {
this.resize();
}
@HostListener('keyup') onKeyUp() {
this.resize();
}
private resize() {
this.el.nativeElement.setAttribute(
'size',
this.el.nativeElement.value.length || 2
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment