DirectiveOptional

wrAutofocus

Focus an element on init, or each time a binding turns true.

Installation

Import the class, and @use the entry point in your global styles: without it the component renders unstyled.

import { Component } from '@angular/core';
import { WrButton } from 'ngwr/button';
import { WrAutofocus } from 'ngwr/directives';
import { WrInput } from 'ngwr/input';

@Component({ imports: [WrButton, WrAutofocus, WrInput] })
export class MyComponent {}

Usage

Setting the binding back to false does not blur the element.

<input wrAutofocus placeholder="Focused on init" />
<input [wrAutofocus]="shouldFocus()" />
Set the binding true

API

NameDescriptionTypeDefault
[wrAutofocus]Focus the host on init, or when the binding turns true.booleantrue

See also