KeyEventPlugin forces event handler in `ngZone`, the behavior is different with non key event handler.
See original GitHub issueConsider 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:
- Created 3 years ago
- Reactions:2
- Comments:5 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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 likekeydown.enter
) isoutside
ngZone.In this case, the current behavior is
button click
is outside of ngZone.keydown
is inside ngZone.Which is not consistent. And most cases are like
button
click, so this issue and PR just makekeydown.enter
behaves the same way asbutton click
.Of course we can discuss this behavior is correct/confuse or not in another issue.
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.