2017-09-29 29 views
0

我必須在加載模態時關注輸入,但如果我使用html5屬性autofocus它似乎不工作。 它有可能使其工作?自動對焦在模態角4不起作用

<div class="col-md-6"> 
<div class="form-group label-floating"> 
    <label class="control-label"> 
    {{'ticketbundle.busroutedetail.dialog.labels.buslinename' 
    | translate }}<span class="star">*</span> 
    </label><input autofocus class="form-control" id="name" required 
    [(ngModel)]="busRouteDetail.name" name="name" #busRouteDetailname> 
</div> 
</div> 

我使用的角材2

回答

0

你可以設置一個本地模板裁判與#myInput和您的TS文件訪問與@ViewChild() myInput元素,並在鉤ngAfterViewInit獲得本地元素,並將焦點與 this.myInput.nativeElement.focus()

另一種選擇屬性是建立一個聚焦指令並將其綁定到該元件的焦點屬性like this

1

如果您正在使用材料對話框,則可以使用cdk-focus-initial來指定您希望初始焦點的位置。

<input cdk-focus-initial type="text"> 

看到這個EXAMPLE

相關問題