我正在为我的应用程序使用Angular v8。我的问题是我想限制用户退出全屏模式。我在 Stack Overflow 上查看了各种解决方案并找到了一个。下面是我尝试实现的代码:
ngOnInit() {
this.attachEventListeners();
}
attachEventListeners() {
document.addEventListener('fullscreenchange', this.onFullScreenChange(), false);
document.addEventListener('webkitfullscreenchange', this.onFullScreenChange(), false);
document.addEventListener('mozfullscreenchange', this.onFullScreenChange(), false);
}
onFullScreenChange() {
const fullscreenElement = document.fullscreenElement || document.mozFullScreenElement || document.webkitFullscreenElement;
console.log(fullscreenElement);
// if in fullscreen mode fullscreenElement won't be null
}
但是这段代码抛出了一个错误:
ExamComponent_Host.ngfactory.js? [sm]:1 ERROR TypeError: Cannot read property 'instance' of undefined
at nodeValue (core.js:27236)
at Object.eval [as handleEvent] (ExamComponent_Host.ngfactory.js? [sm]:1)
at handleEvent (core.js:38098)
at callWithDebugContext (core.js:39716)
at Object.debugHandleEvent [as handleEvent] (core.js:39352)
at dispatchEvent (core.js:25818)
at core.js:37030
at platform-browser.js:1789
at ZoneDelegate.invokeTask (zone-evergreen.js:391)
at Object.onInvokeTask (core.js:34182)
我不知道是否有使用@HostListener
for angular 的东西来处理这个问题。请帮忙。