Ngx-bootstrap: How to autofocus button on Modal?

Created on 28 Mar 2017  路  3Comments  路  Source: valor-software/ngx-bootstrap

I want autofocus to button on Modal
like this:

....
<div class="modal-footer">
<button [autofocus] id="btnDelete" type="button" class="btn btn-success" (click)="active()">OK</button>
</div>

but It not show, so how I can focus on button on Modal?

question

All 3 comments

I afraid I don't know

I faced the same issue, to autofocus the input element.

I found workaround

<button class="btn btn-success btn sp-action-button" (click)="inputNameFocus(); NameModal.show() ">Open modal</button>

<div class="modal-body">
   <label>Name</label>
   <input type="text" id="inputName" name="inputName"  placeholder="Name" [(ngModel)]="name" >
</div>

inputNameFocus() {
  setTimeout(() => {
     document.getElementById('inputName').focus();
  }, 500);
}

I got i worked by calling the focus() on the onShown event.
this.modal.onShown.pipe( tap(() => (document.querySelector('[autofocus]') as HTMLElement).focus()) ).subscribe());

Was this page helpful?
0 / 5 - 0 ratings

Related issues

srjkrl20011991 picture srjkrl20011991  路  3Comments

ctrl-brk picture ctrl-brk  路  3Comments

PascalHonegger picture PascalHonegger  路  3Comments

RolfVeinoeSorensen picture RolfVeinoeSorensen  路  3Comments

phmello picture phmello  路  3Comments