I'm pretty new to Angular and RxJS. I'm trying to listen for a change event on all mat-radio-group elements in a document. I can't understand why using the @HostListener I can catch the event and log it to the console but not using fromEvent
@Directive({selector: "mat-radio-group"})
export class RadioDirective implements AfterViewInit {
constructor(private element: ElementRef) {}
@HostListener('change', ['$event'])
onChange(event){
console.log(event)
}
ngAfterViewInit() {
fromEvent(this.element.nativeElement, 'change')
.subscribe(event => console.log(event))
}
}
I suspect is something related to the ElementRef but I don't really know what I'm missing. I've used fromEvent for others components and didn't have any problem catching events. I would like to mantain fromEvent because is great for manipulating events using .pipe()
EDIT
If I swap the event with click
, fromEvent works perfectly