I have a custom directive that only accepts a number. When I enter 1.2, thatβs good. But when I print only 1. I want to erase the entered 1. to blur. I tried several ways to set the value to an empty string inside onBlur , but still no luck. Here is the code snippet:
@Output() ngModelChange = new EventEmitter(); constructor(el: ElementRef, public model: NgControl){ //Instead of NgControl, I have also tried the NgModel but it did not work this.el = el.nativeElement; } @HostListener('blur', ['$event']) onBlur($event){ if(isNaN(this.el.value.replace(/(,)/g, '')) || this.el.value[this.el.value.length - 1] === '.' || this.el.value[this.el.value.length - 1] === '-'){ this.el.value = ''; this.model.control.updateValue(''); this.model.viewToModelUpdate(''); this.ngModelChange.emit(''); } }
This erases 1. in the input field. But when I print the ngModel value in which the data is stored, the value is 1
In this case, I want to set the ngModel value to an empty string. How can I do this inside the blur?
Thanks!
source share