I struggled with the same problem. I used F6 to open a dialog, and I could not get <input> to get focus. It turned out that I did not interfere with the default behavior of F6 and F6 highlights the browser URL window; so he stole the trick.
switch (event.keyCode) { case 117: event.preventDefault(); this.openAddAveragesDialog(); break; default: return; }
In addition, the magic tag attribute does not work. Autofocus, Focus, Focus, anything, without cubes. I had to create a directive and use it in my input element. I got help with this using this answer.
Here is the element after adding the directive (numberOnly is another directive only for entering a number):
<md-input-container> <input mdInput [focus]="true" [numberOnly]="true"/></md-input-container>
** Change: adding directive code as suggested by @Mackelito for clarity. Here is a directive that I wrote using the answer I linked above. Remember that the material changed the 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(); } }
source share