I use window.addEventListener
in a AngularJs controller to bind event listener, but the problem is that when i go to another state and back to this state, the event listener function called twice and repeats, because of binding event listener multiple times.
This is my controller code:
window.addEventListener("message", receivePosMessage, false);
I want to receivePosMessage
invoked after window.postMessage
. so i used addEventListener
.
How can i solve this problem?
UPDATE
I also added window.removeEventListener("message", receivePosMessage, false);
before addEventListener
but didn't worked!
The problem solved with adding theses below code into controller:
$scope.$on('$destroy', function() {
window.removeEventListener("message", receivePosMessage, false);
});
This solution was given by dear @cody-mikol.