diff --git a/packages/core/src/render3/instructions/listener.ts b/packages/core/src/render3/instructions/listener.ts index 37057efc1f..07faa7eefd 100644 --- a/packages/core/src/render3/instructions/listener.ts +++ b/packages/core/src/render3/instructions/listener.ts @@ -119,6 +119,11 @@ function listenerInternal( const firstCreatePass = tView.firstCreatePass; const tCleanup: false|any[] = firstCreatePass && (tView.cleanup || (tView.cleanup = [])); + // When the ɵɵlistener instruction was generated and is executed we know that there is either a + // native listener or a directive output on this element. As such we we know that we will have to + // register a listener and store its cleanup function on LView. + const lCleanup = getCleanup(lView); + ngDevMode && assertNodeOfPossibleTypes( tNode, TNodeType.Element, TNodeType.Container, TNodeType.ElementContainer); @@ -129,7 +134,6 @@ function listenerInternal( const native = getNativeByTNode(tNode, lView) as RElement; const resolved = eventTargetResolver ? eventTargetResolver(native) : EMPTY_OBJ as any; const target = resolved.target || native; - const lCleanup = getCleanup(lView); const lCleanupIndex = lCleanup.length; const idxOrTargetGetter = eventTargetResolver ? (_lView: LView) => eventTargetResolver(unwrapRNode(_lView[tNode.index])).target : @@ -195,7 +199,6 @@ function listenerInternal( if (processOutputs && outputs !== null && (props = outputs[eventName])) { const propsLength = props.length; if (propsLength) { - const lCleanup = getCleanup(lView); for (let i = 0; i < propsLength; i += 2) { const index = props[i] as number; ngDevMode && assertDataInRange(lView, index); @@ -218,7 +221,7 @@ function listenerInternal( } function executeListenerWithErrorHandling( - lView: LView, tNode: TNode, listenerFn: (e?: any) => any, e: any): boolean { + lView: LView, listenerFn: (e?: any) => any, e: any): boolean { try { // Only explicitly returning false from a listener should preventDefault return listenerFn(e) !== false; @@ -261,13 +264,13 @@ function wrapListener( markViewDirty(startView); } - let result = executeListenerWithErrorHandling(lView, tNode, listenerFn, e); + let result = executeListenerWithErrorHandling(lView, listenerFn, e); // A just-invoked listener function might have coalesced listeners so we need to check for // their presence and invoke as needed. let nextListenerFn = (wrapListenerIn_markDirtyAndPreventDefault).__ngNextListenerFn__; while (nextListenerFn) { // We should prevent default if any of the listeners explicitly return false - result = executeListenerWithErrorHandling(lView, tNode, nextListenerFn, e) && result; + result = executeListenerWithErrorHandling(lView, nextListenerFn, e) && result; nextListenerFn = (nextListenerFn).__ngNextListenerFn__; }