Skip to content
Open
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
44 changes: 44 additions & 0 deletions adev/src/content/reference/errors/NG05102.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Unsupported event target

Angular only recognizes three string values as global event targets: `'window'`, `'document'`, and `'body'`. If you pass anything else as a string target when registering a global event listener, Angular can't resolve it and throws this error.

## Debugging the error

Check for a typo in the target name used with `@HostListener` or `Renderer2.listen()`. A small mistake like `'windw'` instead of `'window'` is enough to trigger this:

```angular-ts
@Component({
selector: 'app-example',
template: '<button>Click me</button>',
})
export class ExampleComponent {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Little NIT:

Suggested change
export class ExampleComponent {
export class Example {

@HostListener('windw:resize') // typo — should be 'window'
onResize() {}
}
```

The same applies when calling `Renderer2.listen()` directly with a string that Angular doesn't know about:

```typescript
this.renderer.listen('global', 'click', handler); // 'global' is not supported
```

## Fixing the error

Correct the target to one of the three supported strings, or pass an actual DOM element instead of a string:

```angular-ts
@Component({
selector: 'app-example',
template: '<button>Click me</button>',
})
export class ExampleComponent {
@HostListener('window:resize')
onResize() {}
}
```

```typescript
// Use an element reference when you need to target something more specific
this.renderer.listen(this.elementRef.nativeElement, 'click', handler);
```
2 changes: 1 addition & 1 deletion goldens/public-api/platform-browser/errors.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const enum RuntimeErrorCode {
// (undocumented)
UNEXPECTED_SYNTHETIC_PROPERTY = 5105,
// (undocumented)
UNSUPPORTED_EVENT_TARGET = 5102,
UNSUPPORTED_EVENT_TARGET = -5102,
// (undocumented)
UNSUPPORTED_ZONEJS_INSTANCE = -5000
}
Expand Down
2 changes: 1 addition & 1 deletion packages/platform-browser/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const enum RuntimeErrorCode {
// misc errors (5100-5200 range)
BROWSER_MODULE_ALREADY_LOADED = 5100,
NO_PLUGIN_FOR_EVENT = 5101,
UNSUPPORTED_EVENT_TARGET = 5102,
UNSUPPORTED_EVENT_TARGET = -5102,
TESTABILITY_NOT_FOUND = 5103,
ROOT_NODE_NOT_FOUND = -5104,
UNEXPECTED_SYNTHETIC_PROPERTY = 5105,
Expand Down
Loading