I have created a directive for an input field. I want to trigger a flag in another component when the input is clicked or focused upon.
@Directive({
selector: '[appDatepicker]'
})
export class DatepickerDirective implements DoCheck{
constructor(private el: ElementRef, private transport: TransportData<boolean>) {
}
ngDoCheck(): void {
let element = this.el.nativeElement;
if (element.onfocus !== null || element.onclick !== null) {
this.transport.setListValue(true);
} else {
this.transport.setListValue(false);
}
}
}
Here is my input with the appDatepicker directive applied.
<input appDatepicker>
Upon loading the page, the attributes onfocus
and onclick
are initially null as expected. However, the issue arises when clicking on the input field - nothing happens. How can I listen for input events within the directive in order to set the BehaviorSubject
?