I was struggling with the same issue. I was using F6 to open the dialog and I could not get the <input>
to get focus. It turned out that I wasn't preventing the default behavior of F6 and F6 highlights the browser URL window; so it was stealing focus.
switch (event.keyCode) {
case 117:
event.preventDefault();
this.openAddAveragesDialog();
break;
default:
return;
}
Also, no magic tag attribute works. Autofocus, Focused, Focus, whatever, no dice. I had to create a directive and use that in my input element. I got help with that using this answer.
Here is the element after adding the directive (numberOnly is another directive for only number input):
<md-input-container> <input mdInput [focus]="true" [numberOnly]="true"/></md-input-container>
**Edit : Adding Directive code as suggested by @Mackelito for clarity.
Here is the directive I wrote using the answer I linked above. Keep in mind material has changed their tag labels to <input matInput>
from <input md-input>
import {Directive, ElementRef, Inject, Input, OnChanges, OnInit, Renderer} from '@angular/core';
@Directive({
selector: '[focus]'
})
export class FocusDirective implements OnChanges, OnInit {
@Input()
focus: boolean;
constructor(@Inject(ElementRef) private element: ElementRef, public renderer: Renderer) {}
ngOnInit() {
this.renderer.invokeElementMethod(this.element.nativeElement, 'focus', []);
}
public ngOnChanges() {
this.element.nativeElement.focus();
}
}