File

src/app/shared/directives/milliseconds.directive.ts

Implements

OnInit

Metadata

Selector [cdMilliseconds]

Index

Methods
Inputs
HostListeners

Constructor

constructor(control: NgControl, formatter: FormatterService)
Parameters :
Name Type Optional
control NgControl No
formatter FormatterService No

Inputs

ngDataReady
Type : EventEmitter<any>

HostListeners

blur
Arguments : '$event.target.value'
blur(value)

Methods

ngOnInit
ngOnInit()
Returns : void
setValue
setValue(value: string)
Parameters :
Name Type Optional
value string No
Returns : void
import { Directive, EventEmitter, HostListener, Input, OnInit } from '@angular/core';
import { NgControl } from '@angular/forms';

import { FormatterService } from '../services/formatter.service';

@Directive({
  selector: '[cdMilliseconds]'
})
export class MillisecondsDirective implements OnInit {
  @Input()
  ngDataReady: EventEmitter<any>;

  constructor(private control: NgControl, private formatter: FormatterService) {}

  setValue(value: string): void {
    const ms = this.formatter.toMilliseconds(value);
    this.control.control.setValue(`${ms} ms`);
  }

  ngOnInit(): void {
    this.setValue(this.control.value);
    if (this.ngDataReady) {
      this.ngDataReady.subscribe(() => this.setValue(this.control.value));
    }
  }

  @HostListener('blur', ['$event.target.value'])
  onUpdate(value) {
    this.setValue(value);
  }
}

result-matching ""

    No results matching ""