import {Directive, ElementRef, HostListener} from '@angular/core'; @Directive({ selector: '[appRemoveSpace]' }) export class RemoveSpaceDirective { constructor(private _el: ElementRef) { } @HostListener('textarea', ['$event']) onInputChange(event) { const initialValue = this._el.nativeElement.value; this._el.nativeElement.value = initialValue.toString().trim(); if ( initialValue !== this._el.nativeElement.value) { event.stopPropagation(); } } }