I have created a simple directive to trim my input text (I plan to expand it's functionality later - so please don't recommend a simple onkeyup function), I would like to make a directive work.
I use my directive like this:
<input type="text" class="form-control" inputTextFilter [(ngModel)]="data.name">
and my directive is as follows:
import { Directive, HostBinding, HostListener } from '@angular/core';
@Directive({
selector: '[inputTextFilter]'
})
export class InputTextFilterDirective {
value: string;
constructor() {
console.log('contrusted InputTextFilterDirective');
this.value = '';
}
@HostListener('change')
onChange($event) {
console.log('in change InputTextFilterDirective');
this.value = $event.target.value.trim();
console.log(this.value);
}
}
I see the constructor message log to the window, but the on change message never appears, and my value never changes (spaces aren't trimmed from end). I suspect hostListeners and hostProperties aren't right as I've seen lots of conflicting examples...but can't confirm a right way.
What's wrong with this?
[ngModel]="data.name | trimPipe" (ngModel)="data.name=$event"
– Affright