I am developing a component in Angular2 (Beta 8). The component has a textbox and a dropdown. I would like to set the focus in textbox as soon as component is loaded or on c
Directive for autoFocus first field
import {
Directive,
ElementRef,
AfterViewInit
} from "@angular/core";
@Directive({
selector: "[appFocusFirstEmptyInput]"
})
export class FocusFirstEmptyInputDirective implements AfterViewInit {
constructor(private el: ElementRef) {}
ngAfterViewInit(): void {
const invalidControl = this.el.nativeElement.querySelector(".ng-untouched");
if (invalidControl) {
invalidControl.focus();
}
}
}