Im creating a directive which is added on an text input, it needs to format the currency inputted.
I have two HostListeners; blur and focus. Upon blur event, I grab the value, format it and assign it to the ngControl. Upon focus, I unformat it and assign it back.
I am injecting ngControl, I can use the valuechanges on it and format it, but some other code sets the value with emitEvent false, so this doesn't get triggered. hence it doesnt format it.
Im looking to see if there is an event that can be used which gets triggered with emitEvent false.
constructor(private currencyPipe: CurrencyPipe, private ngControl: NgControl) {}
@HostListener('blur', ['$event']) format(event) {
let value = event.target.value;
if (value ) {
value = this.format(value );
this.ngControl.control.setValue(value , { emitEvent: false });
}
private format(value: any) {
if (value && value !== '-' && !isNaN(value)) {
return this.currencyPipe.transform(value, this.currencyCode, '', this.digitsInfo, this.locale);
}
return value;
}
HTML
<mat-form-field color="accent" [appearance]="formFieldAppearance" class="table-column-input mr-n1">
<input
autocomplete="off"
appCurrencyFormatter
matInput
formControlName="exchangeRate"
/>
</mat-form-field>
@HostListener('blur', ['$event'])- ShankformatCurrencyfrom@angular/commonrather than injecting thecurrencyPipe. I don't see why the currencyPipe would cause any issues so I don't think that's the issue. It would be helpful to have a stackblitz for this issue. - garbearformControl.setValue(100000, {emitEvent: false});the HostListener wont get triggered - ShankformControl.setValue()to trigger a DOM event? - garbear