This project demonstrates that in Android app @HostListener()
on some document
events doesn't work.
Project structure:
src/app/home/
- bug demo. See the comments inhome.page.ts
for additional info.src/app/zone-run/
-NgZone.run()
workaround.src/app/change-detector-ref/
-ChangeDetectorRef
workaround.
Below I'll shortly explain the problem and workarounds
Add a method like this to the page or component:
@HostListener("document:click")
onDocumentClick() {
this.documentClickCount++;
}
And output documentClickCount
in your HTML.
When you tap on the screen, value should update.
You'll see that it updates in a browser on both PC and Android, but doesn't in an Android app (apk).
The same thing happens with document:pointerdown
and document:pointerup
events. Maybe more events are affected.
I suspect that change detection is not triggered in such @HostListener
-s for some reason. This is backed by the fact that provided workarounds work.
I prefer this one since you just need to wrap your code in it.
constructor(private zone: NgZone) {}
@HostListener("document:click")
onDocumentClick() {
this.zone.run(() => this.documentClickCount++);
}
With this you'll need to call ChangeDetectorRef.detectChanges()
every time you return
from your handler.
constructor(private changeDetectorRef: ChangeDetectorRef) {}
@HostListener("document:click")
onDocumentClick() {
this.documentClickCount++;
this.changeDetectorRef.detectChanges();
}