Press "Enter" to skip to content

Angular Directive To Fix Autofocus On Dynamically Generated Controls

ngx-autofocus-fix is ​​an Angular 5+ directive to fix autofocus on dynamically generated controls. Uses [Autofocus] selector, no need to change your HTML template.

autofocus attribute angular, dynamically set focus javascript, set autofocus in javascript, input autofocus not working, autofocus input angular, ngx autofocus fix

Advantages over other libraries

  • Uses native HTML property autofocus as delimiter!
  • No custom delimiters, no need to change your HTML template.
  • Works with the original DOM. It doesn’t use any dependencies (jQuery, Lodash, etc.).
  • Can use input attributes or provide global options via AutofocusFixConfig
  • 100% coverage, over 60 test units.
  • E2E tests for 8,7,6 and 5 versions of Angular including the e2e test for Angular Material Input.
  • The library understands an extensive list of input data. (null / NaN / ‘true’ / [] / …).
  • Supports asynchronous focus (optionally wrapping. focus() executed with setTimeout()).
  • Works perfectly with angular materials. (There is an E2E test)
  • It works with AOT mode.

How to make use of it:

Installation:

# Yarn
$ yarn add ngx-autofocus-fix

# NPM
$ npm install ngx-autofocus-fix --save

Import the library in your Angular app, for example in the AppModule:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AutofocusFixModule } from 'ngx-autofocus-fix'; // <--- new code

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,

    AutofocusFixModule.forRoot(), // <--- new code
  ],
  providers: [],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

You can now use the autofocus directive in app.component.html

<input autofocus
       placeholder="I have autofocus"
       *ngIf="showInput"
>
<button (click)="showInput = !showInput">Toggle Input</button>

Fix Autofocus On Dynamically Created Controls, ngx-autofocus-fix Plugin/Github, autofocus button angular


See Demo And Download

Official Website(korniychuk): Click Here

This superior jQuery/javascript plugin is developed by korniychuk. For extra advanced usage, please go to the official website.

Be First to Comment

    Leave a Reply

    Your email address will not be published.