4

How do I namespace my DOM events in AngularJS?

I have some directives that bind mouse events on the Document:

$document.on('mousemove');
$document.on('mouseup');

After mouseup I do:

$document.off('mousemove');
$document.off('mouseup');

And then some other directives does not work, because events were destroyed.

In Angular's documentation for angular.element is written:

on() — Does not support namespaces, selectors or eventData

Well, but I think I need event namespaces to target which events to remove. So, what should I do?

2
  • 1
    How about removing specific handlers then? Commented Feb 3, 2015 at 21:18
  • OMG, I forgot about additional parameters in .off() method (facepalm). It works. Make your comment an answer, I will accept it. Commented Feb 3, 2015 at 21:32

1 Answer 1

6

Even though jqLite doesn't support namespaces (yet, at least), you can always use .off() with a handler param, to specify which listener you should remove. For example:

function mouseMoveHandler() {
  // do some stuff
}
$document.on('mousemove', mouseMoveHandler);
// ... and later on
$document.off('mousemove', mouseMoveHandler);
Sign up to request clarification or add additional context in comments.

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.