Trim-value-accessor for ionic

Hi guys,

I’m trying to add a trim-value-accessor(trim the leading and trailing spaces from an input field) in my ionic app. I’m using the following code but it’s not working. Can anybody help me with this?

Thanks

import { Directive, HostListener, forwardRef, Renderer2, ElementRef } from '@angular/core';
import { DefaultValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';

const TRIM_VALUE_ACCESSOR: any = {
    provide: NG_VALUE_ACCESSOR,
    useExisting: forwardRef(() => TrimValueAccessorDirective),
    multi: true
};

/**
 * The trim accessor for writing trimmed value and listening to changes that is
 * used by the {@link NgModel}, {@link FormControlDirective}, and
 * {@link FormControlName} directives.
 */
@Directive({
    selector: `
    ion-input:not([type=checkbox]):not([type=radio]):not([type=password]):not([readonly]):not(.ng-trim-ignore)[formControlName],
    ion-input:not([type=checkbox]):not([type=radio]):not([type=password]):not([readonly]):not(.ng-trim-ignore)[formControl],
    ion-input:not([type=checkbox]):not([type=radio]):not([type=password]):not([readonly]):not(.ng-trim-ignore)[ngModel],
    ion-textarea:not([readonly]):not(.ng-trim-ignore)[formControlName],
    ion-textarea:not([readonly]):not(.ng-trim-ignore)[formControl],
    ion-textarea:not([readonly]):not(.ng-trim-ignore)[ngModel],
    :not([readonly]):not(.ng-trim-ignore)[ngDefaultControl]'
  `,
    providers: [TRIM_VALUE_ACCESSOR]
})
export class TrimValueAccessorDirective extends DefaultValueAccessor {

    @HostListener('input', ['$event.target.value'])
    ngOnChange = (val: string) => {
      this.onChange(val.trim());
    };
  
    @HostListener('blur', ['$event.target.value'])
    applyTrim (val: string) {
      this.writeValue(val);
    };

    constructor(private renderer: Renderer2, private elRef: ElementRef) {
        super(renderer, elRef, true);
    }

    writeValue(value: any): void {
        if (typeof value === 'string') {
            value = value.trim();
        }
        super.writeValue(value);
    }

}

Did you do it works?