I am creating a custom directive that suppose to reformat the text inside of the md-input. In my directive's ngOnInit and in @HostListener('blur', ['$event.target.value']) I have a logic to reformat the text that was entered by the user. And it is working except when the data is getting bound from the api call. I am wondering what event is getting fired when angular is updating that data so I can listen for it in my directive and fire my format logic for that data as well.
Update 1: added code to clear up things
<input type="text"
mdInput
[(ngModel)]="item.Price"
appMyPriceFormatter
placeholder="Price"
tabindex="5"
[disabled]="disableInputs">
Directive code:
import {Directive, ElementRef, HostListener, OnInit} from '@angular/core';
import {CurrencyPipe} from '@angular/common';
@Directive({
selector: '[appMyPriceFormatter]'
})
export class MyPriceFormatterDirective implements OnInit {
private el: HTMLInputElement;
constructor(private elementRef: ElementRef,
private currencyPipe: CurrencyPipe) {
this.el = this.elementRef.nativeElement;
}
ngOnInit() {
if ((this.el.value !== null) && (this.el.value.trim() !== '')) {
this.el.value = this.currencyPipe.transform(this.el.value, 'USD', true, '1.5-5');
} else {
this.el.value = null;
}
}
@HostListener('focus', ['$event.target.value'])
onFocus(value) {
this.el.value = value.replace(/[^\d\-\.]/g, '');
}
@HostListener('blur', ['$event.target.value'])
onBlur(value) {
if ((value !== null) && (value.trim() !== '')) {
this.el.value = this.currencyPipe.transform(value, 'USD', true, '1.5-5');
} else {
this.el.value = null;
}
}
}