File

src/app/shared/directives/password-button.directive.ts

Implements

OnInit

Metadata

Selector [cdPasswordButton]

Index

Properties
Methods
Inputs
HostListeners

Constructor

constructor(elementRef: ElementRef, renderer: Renderer2)
Parameters :
Name Type Optional
elementRef ElementRef No
renderer Renderer2 No

Inputs

cdPasswordButton
Type : string

HostListeners

click
click()

Methods

Private getInputElement
getInputElement()
Returns : any
ngOnInit
ngOnInit()
Returns : void
Private update
update()
Returns : void

Properties

Private iElement
Type : HTMLElement
import { Directive, ElementRef, HostListener, Input, OnInit, Renderer2 } from '@angular/core';

@Directive({
  selector: '[cdPasswordButton]'
})
export class PasswordButtonDirective implements OnInit {
  private iElement: HTMLElement;

  @Input()
  private cdPasswordButton: string;

  constructor(private elementRef: ElementRef, private renderer: Renderer2) {}

  ngOnInit() {
    this.renderer.setAttribute(this.elementRef.nativeElement, 'tabindex', '-1');
    this.iElement = this.renderer.createElement('i');
    this.renderer.addClass(this.iElement, 'icon-prepend');
    this.renderer.addClass(this.iElement, 'fa');
    this.renderer.appendChild(this.elementRef.nativeElement, this.iElement);
    this.update();
  }

  private getInputElement() {
    return document.getElementById(this.cdPasswordButton) as HTMLInputElement;
  }

  private update() {
    const inputElement = this.getInputElement();
    if (inputElement && inputElement.type === 'text') {
      this.renderer.removeClass(this.iElement, 'fa-eye');
      this.renderer.addClass(this.iElement, 'fa-eye-slash');
    } else {
      this.renderer.removeClass(this.iElement, 'fa-eye-slash');
      this.renderer.addClass(this.iElement, 'fa-eye');
    }
  }

  @HostListener('click')
  onClick() {
    const inputElement = this.getInputElement();
    // Modify the type of the input field.
    inputElement.type = inputElement.type === 'password' ? 'text' : 'password';
    // Update the button icon/tooltip.
    this.update();
  }
}

result-matching ""

    No results matching ""