
Security News
Deno 2.6 + Socket: Supply Chain Defense In Your CLI
Deno 2.6 introduces deno audit with a new --socket flag that plugs directly into Socket to bring supply chain security checks into the Deno CLI.
@fixies/ngx-autofocus-fix
Advanced tools
<!-- BADGES --> [](https://circleci.com/gh/korniychuk/angular-autofocus-fix/tree/master) [.
legacy version for Angular 2/4
Online Demo (Stackblitz.com)

autofocus as the selector! exampleAutofocusFixConfignull/NaN/'true'/[]/...). See Advanced examples.focus() execution with setTimeout()).Notice: npm package renamed angular-autofocus-fix -> ngx-autofocus-fix
To install this library, run:
$ npm i ngx-autofocus-fix --save
or
$ yarn add ngx-autofocus-fix
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 { }
<input autofocus
placeholder="I have autofocus"
*ngIf="showInput"
>
<button (click)="showInput = !showInput">Toggle Input</button>
Ways to disable autofocus: any js-falsy value, except an empty string (default @Input's normalization mode)
<!-- with data binding -->
<input [autofocus]=""> <!-- undefined value -->
<input [autofocus]="undefined">
<input [autofocus]="false">
<input [autofocus]="null">
<input [autofocus]="0">
<input [autofocus]="NaN">
<!-- without data binding -->
<input autofocus="undefined">
<input autofocus="false">
<input autofocus="null">
<input autofocus="0">
<input autofocus="NaN">
<input> <!-- disabled by default -->
Ways to enable autofocus: any js-truthy value and an empty string (default @Input's normalization mode)
<!-- an empty string will enable autofocus, this is default HTML behavior -->
<input [autofocus]="''">
<input autofocus="">
<input autofocus> <!-- this is an empty string too -->
<input autofocus="autofocus">
<input [autofocus]="true">
<input [autofocus]="1">
<input autofocus="true">
<input [autofocus]="'any other values'">
<input autofocus="any other values">
<input [autofocus]="{}">
<input [autofocus]="[]">
All input values are passed through the function: normalizeInputAsBoolean(value: any, smartEmptyCheck = false): boolean.
Smart Empty Check mode changes the behavior so that the following values are treated as falsy:
''{}[]See Configuration to understand how to enable the mode.
Notes:
autofocus attribute. All other directive @Input's always works in the default normalization mode.autofocus without any value doesn't enable autofocusing in Smart Empty Check mode. Because of an empty value means an empty string in terms of Angular templates syntax.export class AutofocusFixConfig {
...
/**
* In case `true` .focus() events will be wrapped by `setTimeout(() => ...)`.
*
* Notice:
* I'm not sure that the action is a good practice, however this ability added because of next issues:
* - https://github.com/korniychuk/angular-autofocus-fix/issues/1
* - https://github.com/spirosikmd/angular2-focus/issues/46
*/
public readonly async: boolean = false;
/**
* In case `true`: treat an empty string, an empty array and an empty object as a falsy value.
* In case `false`(default): each of these values treats as truthy.
*/
public readonly smartEmptyCheck: boolean = false;
/**
* In case `true`: trigger {@link ChangeDetectorRef.detectChanges}() after {@link HTMLElement.focus}().
*
* This is helpful in the case when the HTMLElement to which {@link AutofocusFixDirective} added
* wrapped by another directive/component that has some binding related to focus of the element.
* In this case without enabling .triggerChangeDetection option Angular throws ExpressionChangedAfterItHasBeenCheckedError.
*
* A striking example is the <mat-form-field> from the Angular Material that wraps <input> control.
*/
public readonly triggerDetectChanges: boolean = false;
}
AutofocusFixDirective:1. Specify attribute-options for specific HTML Element
<input type="text"
autofocus
autofocusFixAsync
autofocusFixSmartEmptyCheck
autofocusFixTriggerDetectChanges
>
Normalization(only default) available and binding supported.
<input type="text"
autofocus
[autofocusFixAsync]="true"
[autofocusFixSmartEmptyCheck]="true"
[autofocusFixTriggerDetectChanges]="true"
>
<input type="text"
autofocus
autofocusFixAsync="true"
[autofocusFixSmartEmptyCheck]="isSmart"
autofocusFixTriggerDetectChanges="a truthy value"
>
2. Specify global options for the whole application by passing it to .forRoot({ ... })
@NgModule({
...
imports: [
...
AutofocusFixModule.forRoot({
async: true,
smartEmptyCheck: true,
triggerDetectChanges: true,
}),
],
...
})
export class AppModule { }
3. Provide Lazy-Route level AutofocusFixConfig config
import { NgModule } from '@angular/core';
import { AutofocusFixModule, AutofocusFixConfig } from 'ngx-autofocus-fix';
const autofocusFixConfigProvider: Provider = {
provide: AutofocusFixConfig,
useValue: new AutofocusFixConfig({
async: true,
smartEmptyCheck: true,
triggerDetectChanges: true,
}),
};
@NgModule({
...
imports: [
...
AutofocusFixModule,
],
providers: [ autofocusFixConfigProvider ],
})
export class MyLazyLoadableModule { }
4. Provide Component level AutofocusFixConfig config
import { Component, Provider } from '@angular/core';
import { AutofocusFixConfig } from 'ngx-autofocus-fix';
const autofocusFixConfigProvider: Provider = {
provide: AutofocusFixConfig,
useValue: new AutofocusFixConfig({
async: true,
smartEmptyCheck: true,
triggerDetectChanges: true,
}),
};
@Component({
...
providers: [ autofocusFixConfigProvider ],
})
export class MyComponent {}
Build the library:
$ npm run build
Publish the library:
cd dist/ngx-autofocus-fix
npm publish
To lint all *.ts files:
$ npm run lint
To run library unit-tests:
$ npm run test-lib
To run e2e tests:
$ npm run e2e -- --prod=true
To run local dev server http://localhost:4200:
$ npm start
$ npm run serve:prod -- angular-8-test # AoT & Prod env
Anton Korniychuk |
|---|
FAQs
<!-- BADGES --> [](https://circleci.com/gh/korniychuk/angular-autofocus-fix/tree/master) [
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Deno 2.6 introduces deno audit with a new --socket flag that plugs directly into Socket to bring supply chain security checks into the Deno CLI.

Security News
New DoS and source code exposure bugs in React Server Components and Next.js: what’s affected and how to update safely.

Security News
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.