0

I know how to bind to events using the HostListener decorator like this:

  @HostListener('document:mousemove', ['$event'])
  onMousemove(event) {
    //Some code on mouse movement.
  }

But I would like to be able to bind and unbind to the mousemove event intermittently throughout the lifecycle of a component. I don't know what this type of binding is called, and I can't find anything about it. Should I try to use native JavaScript event binding?

2
  • 1
    Can you further explain the context of the problem? Commented Feb 28, 2017 at 4:05
  • 1
    You can only unbind yourself if you register imperatively. If you use the declarative approach, there is no way to do that. Commented Feb 28, 2017 at 6:35

2 Answers 2

1

HTML:

<div (mouseover)="someOverFunction()" 
     (mouseleave)="someLeaveFunction()">
         <span *ngIf="mouseOverDiv == true">hello mouseover</span>
         <span *ngIf="mouseOverDiv == false">hello mouseleave</span>
</div>

TS:

mouseOverDiv : boolean = false;

someOverFunction(){
 this.mouseOverDiv = true;  
}

someLeaveFunction(){
 this.mouseOverDiv = false;  
}
Sign up to request clarification or add additional context in comments.

Comments

0

Define a output variable like below.

 @Output() mouseEvent = new EventEmitter();

Use this variable when you want to emit the event like below.

 this.mouseEvent.emit({value:paramValue});

Call that mouse event where you want to add in html element

 <HTMLElement (mouseEvent) = "methodName()"></HTMLElement>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.