Skip to content

Instantly share code, notes, and snippets.

@TheLarkInn
Last active April 24, 2016 16:01
Show Gist options
  • Save TheLarkInn/d3b0ee4d3647331b70d07b5519a12f09 to your computer and use it in GitHub Desktop.
Save TheLarkInn/d3b0ee4d3647331b70d07b5519a12f09 to your computer and use it in GitHub Desktop.
A more successful way to implement the addEventListener function for Multiple events bindings.
addEventListener(element: HTMLElement, eventName: string, handler: Function): Function {
var zone = this.manager.getZone();
var eventsArray = this.getMultiEventArray(eventName);
// Entering back into angular to trigger changeDetection
var outsideHandler = (event) => {
zone.run(() => handler(event))
};
// Executed outside of angular so that change detection is not constantly triggered.
var addAndRemoveHostListenersForOutsideEvents = () => {
eventsArray.forEach((singleEventName: string) => {
// Sending the events back up to this.manager allows the events to be handled by the appropriate plugin
this.manager.addEventListener(element, singleEventName, outsideHandler);
});
}
return this.manager.getZone().runOutsideAngular(addAndRemoveHostListenersForOutsideEvents);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment