Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

autofocus #5582


User avatar
0
gconey created

Hi,

Using the Angular+Core version of the FW. The autofocus directive doesn't work for text boxes in modals. I think this is because it tries to set the focus while the modal is animating open. I want to help my users by using this directive to set focus to the 1st field.

I have got it to work by adding a timeout to the auto-focus directive as follows but I think there may be a better way to fix this. Any ideas?

ngAfterViewInit(): void {
       window.setTimeout(() => {
            $(this._element.nativeElement).focus();
       },500);

1 Answer(s)
  • User Avatar
    0
    alper created
    Support Team

    you can use onShown

    onShown(): void {
            $(this.currentPasswordInput.nativeElement).focus();
    }
    

    See the built-in modal for the same purpose; <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero-core/blob/f72d4bf9ced778e06265f1c415a8553b10cbaf3d/angular/src/app/shared/layout/profile/change-password-modal.component.ts#L44">https://github.com/aspnetzero/aspnet-ze ... ent.ts#L44</a>