1

I'm looking for a way to release an event when linked on this way:

angular.element($document[0].body).on('click', function (e) {
           // code
});

Any clue?

3
  • angular.element($document[0].body).off('click'); Commented Oct 13, 2014 at 14:35
  • wow that was crazy fast! Let me try that Commented Oct 13, 2014 at 14:36
  • Be aware that will disable all click handlers for that selector, that were registered with 'on'. Of course, if that is the only one then no problem. Commented Oct 13, 2014 at 14:40

1 Answer 1

3
// handler
var handler = function (e) { }    

angular.element($document[0].body).on('click', handler);


// afterwards
angular.element($document[0].body).off('click', handler);

Also check if you're using jQuery or jqLite (https://docs.angularjs.org/api/ng/function/angular.element, Angular's implementation). You might try to do unsupported things if you use the latter.

Sign up to request clarification or add additional context in comments.

3 Comments

The OPs code is using an anon function so Im not sure this will work if he can't refactor code to avoid that.
+1 I guess best practice is to separate out anon handler to named function and set the reference. So that you are turning off only that click handler.
+1, I like surgical solutions that don't suprise me in the future.

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.