I want to use a directive to transform all input data to uppercase. To achieve that, I create this custom directive :
@Directive({
selector: \'[appToUpperC
import { Directive, ElementRef, HostListener } from '@angular/core';
@Directive({
selector: '[lowercaser]'
})
export class LowerCaserDirective {
lastValue: string;
constructor(public ref: ElementRef) {
}
@HostListener('input', ['$event']) onInput(event) {
const resEventValue = event.target.value.toLowerCase();
// Avoid max call
if (!this.lastValue || (this.lastValue && event.target.value.length > 0 && this.lastValue !== resEventValue)) {
this.lastValue = this.ref.nativeElement.value = resEventValue;
// Propagation
const evt = document.createEvent('HTMLEvents');
evt.initEvent('input', false, true);
event.target.dispatchEvent(evt);
}
}
}