File

packages/ui/src/lib/utility/focus/focus.directive.ts

Implements

OnInit AfterViewInit

Metadata

Selector [ecFocus]

Index

Methods
Inputs

Constructor

constructor(element: ElementRef)
Parameters :
Name Type Optional
element ElementRef No

Inputs

autofocus
Type : boolean
ecFocus
Type : EventEmitter<boolean>

Methods

ngAfterViewInit
ngAfterViewInit()
Returns : void
ngOnInit
ngOnInit()
Returns : void
import { Directive, ElementRef, EventEmitter, Input, OnInit, AfterViewInit } from '@angular/core';

@Directive({
  // tslint:disable-next-line:directive-selector
  selector: '[ecFocus]',
})
export class FocusDirective implements OnInit, AfterViewInit {
  @Input() ecFocus: EventEmitter<boolean>;
  @Input() autofocus: boolean;

  constructor(private element: ElementRef) {}

  ngAfterViewInit() {
    if (this.autofocus) {
      this.ecFocus.emit(true);
    }
  }

  ngOnInit() {
    this.ecFocus.subscribe((event: boolean) => {
      if (event) {
        this.element.nativeElement.focus();
      } else {
        this.element.nativeElement.blur();
      }
    });
  }
}

result-matching ""

    No results matching ""