I am trying to create my first directive. What I want is to simply prevent typing on a text field.
This is what I have tried:
import { Directive, ElementRef, HostListener, Input } from '@angular/core';
import { NgControl } from '@angular/forms';
@Directive({
selector: 'input[numbersOnly]'
})
export class NumberDirective {
constructor(private _el: ElementRef) { }
@HostListener('input', ['$event']) onInputChange(event) {
event.stopPropagation();
}
}
<input type="text" ngModel numbersOnly>
But the user is still able to type.
I also tried it with event.preventDefault();
but that also didn't work. What am I doing wrong?