Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions packages/core/src/event_delegation_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,25 @@ export class GlobalEventDelegation implements OnDestroy {
return isEarlyEventType(eventType);
}

addEventListener(element: HTMLElement, eventName: string, handler: Function): Function {
this.eventContractDetails.instance!.addEvent(eventName);
sharedStashFunction(element, eventName, handler);
getActionCache(element)[eventName] = '';
return () => this.removeEventListener(element, eventName, handler);
addEventListener(element: HTMLElement, eventType: string, handler: Function): Function {
// Note: contrary to the type, Window and Document can be passed in
// as well.
if (element.nodeType === Node.ELEMENT_NODE) {
this.eventContractDetails.instance!.addEvent(eventType);
getActionCache(element)[eventType] = '';
sharedStashFunction(element, eventType, handler);
} else {
element.addEventListener(eventType, handler as EventListener);
Comment thread
iteriani marked this conversation as resolved.
Outdated
}
return () => this.removeEventListener(element, eventType, handler);
}

removeEventListener(element: HTMLElement, eventType: string, callback: Function): void {
getActionCache(element)[eventType] = undefined;
if (element.nodeType === Node.ELEMENT_NODE) {
getActionCache(element)[eventType] = undefined;
} else {
element.removeEventListener(eventType, callback as EventListener);
}
}
}

Expand Down
34 changes: 33 additions & 1 deletion packages/core/test/event_dispatch/event_dispatch_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Component, Renderer2, inject, ɵprovideGlobalEventDelegation} from '@angular/core';
import {
Component,
HostListener,
Renderer2,
inject,
ɵprovideGlobalEventDelegation,
} from '@angular/core';
import {ComponentFixture, TestBed} from '@angular/core/testing';

function configureTestingModule(components: unknown[]) {
Expand Down Expand Up @@ -216,5 +222,31 @@ describe('event dispatch', () => {
bottomEl.click();
expect(onClickSpy).toHaveBeenCalledTimes(2);
});
it('should allow host listening on the window', async () => {
const onClickSpy = jasmine.createSpy();
@Component({
standalone: true,
selector: 'app',
template: `
<div id="top">
<div id="bottom"></div>
</div>
`,
})
class SimpleComponent {
renderer = inject(Renderer2);
destroy!: Function;
@HostListener('window:click', ['$event.target'])
listen(el: Element) {
onClickSpy();
}
}
configureTestingModule([SimpleComponent]);
fixture = TestBed.createComponent(SimpleComponent);
const nativeElement = fixture.debugElement.nativeElement;
const bottomEl = nativeElement.querySelector('#bottom')!;
bottomEl.click();
expect(onClickSpy).toHaveBeenCalledTimes(1);
});
});
});