import { Directive , HostListener , Input } from '@angular/core' ; import { NgModel } from '@angular/forms' ; @ Directive ( { selector : '[ngModel][max-number-value]' , } ) export class MaxNumberValueDirective { @ Input ( 'max-number-value' ) public value : string ; constructor ( private readonly model ) { } @ HostListener ( 'ionBlur' , [ '$event.target' ] ) public blur ( ) { let val = this . model . control . value ; val = val + '' ; if ( val === '' ) { val = '0' ; } if ( val . indexOf ( ',' ) ) { val = val . replace ( /,/g , '.' ) ; } const parsedFloat = parseFloat ( val ) ; if ( parsedFloat > parseFloat ( this . value ) ) { this . model . control . setValue ( 0 ) ; } else { if ( parsedFloat < 0 ) { this . model . control . setValue ( 0 ) ; } else { if ( Number . isNaN ( parsedFloat ) ) { this . model . control . setValue ( '' ) ; } else { this . model . control . setValue ( parsedFloat ) ; } } } } }