diff --git a/packages/platform-browser/src/dom/events/dom_events.ts b/packages/platform-browser/src/dom/events/dom_events.ts index 618f681e4a..918a121de7 100644 --- a/packages/platform-browser/src/dom/events/dom_events.ts +++ b/packages/platform-browser/src/dom/events/dom_events.ts @@ -154,50 +154,37 @@ export class DomEventsPlugin extends EventManagerPlugin { let callback: EventListener = handler as EventListener; // if zonejs is loaded and current zone is not ngZone // we keep Zone.current on target for later restoration. - if (zoneJsLoaded) { - if (!NgZone.isInAngularZone() || isBlackListedEvent(eventName)) { - let symbolName = symbolNames[eventName]; - if (!symbolName) { - symbolName = symbolNames[eventName] = __symbol__(ANGULAR + eventName + FALSE); - } - let taskDatas: TaskData[] = (element as any)[symbolName]; - const globalListenerRegistered = taskDatas && taskDatas.length > 0; - if (!taskDatas) { - taskDatas = (element as any)[symbolName] = []; - } + if (zoneJsLoaded && (!NgZone.isInAngularZone() || isBlackListedEvent(eventName))) { + let symbolName = symbolNames[eventName]; + if (!symbolName) { + symbolName = symbolNames[eventName] = __symbol__(ANGULAR + eventName + FALSE); + } + let taskDatas: TaskData[] = (element as any)[symbolName]; + const globalListenerRegistered = taskDatas && taskDatas.length > 0; + if (!taskDatas) { + taskDatas = (element as any)[symbolName] = []; + } - const zone = isBlackListedEvent(eventName) ? Zone.root : Zone.current; - if (taskDatas.length === 0) { - taskDatas.push({zone: zone, handler: callback}); - } else { - let callbackRegistered = false; - for (let i = 0; i < taskDatas.length; i++) { - if (taskDatas[i].handler === callback) { - callbackRegistered = true; - break; - } - } - if (!callbackRegistered) { - taskDatas.push({zone: zone, handler: callback}); - } - } - - if (!globalListenerRegistered) { - element[ADD_EVENT_LISTENER](eventName, globalListener, false); - } + const zone = isBlackListedEvent(eventName) ? Zone.root : Zone.current; + if (taskDatas.length === 0) { + taskDatas.push({zone: zone, handler: callback}); } else { - // if zone.js loaded and we are in angular zone, we don't need to - // use zone.js patched addEventListener - const wrappedCallback = function() { - return self.ngZone.run(callback, this, arguments as any); - }; - zoneJsLoaded.apply(element, [eventName, wrappedCallback, false]); - // we just use the underlying removeEventListener - return () => element[REMOVE_EVENT_LISTENER].apply( - element, [eventName, wrappedCallback, false]); + let callbackRegistered = false; + for (let i = 0; i < taskDatas.length; i++) { + if (taskDatas[i].handler === callback) { + callbackRegistered = true; + break; + } + } + if (!callbackRegistered) { + taskDatas.push({zone: zone, handler: callback}); + } + } + + if (!globalListenerRegistered) { + element[ADD_EVENT_LISTENER](eventName, globalListener, false); } } else { - // use zone.js patched addEventListener or native addEventListener if zone.js not loaded element[NATIVE_ADD_LISTENER](eventName, callback, false); } return () => this.removeEventListener(element, eventName, callback); @@ -209,8 +196,6 @@ export class DomEventsPlugin extends EventManagerPlugin { if (!underlyingRemove) { return target[NATIVE_REMOVE_LISTENER].apply(target, [eventName, callback, false]); } - - // if zone.js loaded and wrappedCallback not exists, the callback was added in different zone let symbolName = symbolNames[eventName]; let taskDatas: TaskData[] = symbolName && target[symbolName]; if (!taskDatas) { diff --git a/packages/platform-browser/test/dom/events/event_manager_spec.ts b/packages/platform-browser/test/dom/events/event_manager_spec.ts index fce29c98e9..ea4c35c984 100644 --- a/packages/platform-browser/test/dom/events/event_manager_spec.ts +++ b/packages/platform-browser/test/dom/events/event_manager_spec.ts @@ -283,7 +283,7 @@ export function main() { }); getDOM().dispatchEvent(element, dispatchedEvent); expect(receivedEvents).toEqual([dispatchedEvent, dispatchedEvent]); - expect(receivedZones).toEqual([Zone.root.name, 'angular']); + expect(receivedZones).toEqual([Zone.root.name, 'fakeAngularZone']); receivedEvents = []; remover1 && remover1();