here
.
import { AfterContentInit, Directive, ElementRef, Input } from "@angular/core";
@Directive({
selector: "[autoFocus]"
})
export class AutofocusDirective implements AfterContentInit {
@Input() public appAutoFocus: boolean;
public constructor(private el: ElementRef) {}
public ngAfterContentInit() {
setTimeout(() => {
this.el.nativeElement.focus();
}, 100);
}
}
<input autoFocus aria-label="Username" class="username"
matInput
placeholder="Enter Username" type="text">
如果你在一个页面上多次使用它,最后一次使用的那一个将会被聚焦。