question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KeyEventPlugin forces event handler in `ngZone`, the behavior is different with non key event handler.

See original GitHub issue

Consider the following case.

app.component.html

<button #btnToggle>Toggle</button>
<div *ngIf="show">
  <button (click)="onClick()">Button</button>
  <input (keydown.enter)="onKeydown()"/>
</div>

app.component.ts

export class AppComponent implements AfterViewInit {
    @ViewChild('btnToggle') btnToggle: ElementRef;
    show = false;
    constructor(private zone: NgZone, private cd: ChangeDetectorRef) {}

    ngAfterViewInit() {
      this.zone.runOutsideAngular(() => {
        const el = this.btnToggle.nativeElement as HTMLElement;
        el.addEventListener('click', e => {
          this.show = true;
          this.cd.detectChanges();
        })
      })
   }

   onClick() {
      console.log('button click in ngZone? ', NgZone.isInAngularZone());
   }

   onKeydown() {
      console.log('keydown in ngZone? ', NgZone.isInAngularZone());
    }
}

The live sample is here https://stackblitz.com/edit/angular-hga7lk?file=src%2Fapp%2Fapp.component.ts

After click the toggle button, then click the inside button and press enter inside the input. The output will be:

button click in ngZone?  false
keyDown in ngZone? true

This is not consistent

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
JiaLiPassioncommented, Jul 28, 2020

@artaommahe , I totally understand your concerns and agree with your point, but this issue and related PR doesn’t introduce a new behavior. The current behavior (except the events like keydown.enter) is outside ngZone.

<button #btnToggle>Toggle</button>
<div *ngIf="show">
  <button (click)="onClick()">Button</button>
  <input (keydown.enter)="onKeydown()"/>
</div>

In this case, the current behavior is

  1. the button click is outside of ngZone.
  2. the keydown is inside ngZone.

Which is not consistent. And most cases are like button click, so this issue and PR just make keydown.enter behaves the same way as button click.

Of course we can discuss this behavior is correct/confuse or not in another issue.

0reactions
angular-automatic-lock-bot[bot]commented, Apr 8, 2022

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Running event listeners outside of the NgZone - InDepth.Dev
NgZone notifies Angular when to perform the change detection process (e.g. a DOM event with bound listener is one of the triggerers).
Read more >
Issue with zone.js and non angular dom element events
The problem is that zone patches onclick and uses addEventListener/removeEventListener to register/remove a callback for the button.
Read more >
NgZone - Angular
The DOM event listener can update the data in an Angular component and also trigger change detection, as in the following example. src/app/click-me.component.ts...
Read more >
Adding event listeners outside of the Angular zone - Medium
In this article I'll present how to properly add event listeners outside of the Angular zone, so that the change detection is not...
Read more >
Using Zones in Angular for better performance
Rendering 10.000 boxes is not a super sophisticated task, however, ... In other words, we need to change our mouseDown() event handler to ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found