Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subscribing to a component mouse move event

Tags:

angular

I found code to subscribe to mouse move in documents, how can I subscribe to only the component instead?

ngOnInit() {
  this.subscription = 
       fromEvent(document, 'mousemove')
                         .subscribe(e => {
                           console.log(e);
                         });
}

ngOnDestroy() {
  this.subscription.unsubscribe();
}
like image 844
doe Avatar asked Oct 20 '25 11:10

doe


1 Answers

Use ElementRef

ElementRef grants direct access to the host DOM element through its nativeElement property.

constructor(private ele: ElementRef ){
    const host = fromEvent(this.ele.nativeElement,'click').subscribe(c=>console.log(c));
}

Example:https://stackblitz.com/edit/angular-z5y6sq

Ref:https://angular.io/guide/attribute-directives

like image 141
Chellappan வ Avatar answered Oct 23 '25 02:10

Chellappan வ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!