Angular/TypeScript -当方向为横屏时自动打开全屏,仅适用于移动设备



我想在Angular组件中,当screen.orientation.type.include ('landscape')时打开全屏,并且只对移动设备开放。我不能使用任何事件,如点击。

ngOnInit(): void {
window.addEventListener("orientationchange", function(event) {
const orientation = screen.orientation.type
if (orientation) {
const elem: any = document.documentElement;
const doc: any = document;
elem.requestFullscreen();
elem.mozRequestFullScreen();
elem.msRequestFullscreen();
elem.webkitRequestFullscreen();
}
});

}

有人知道我怎么才能做到这一点吗?

这不是Angular完成此类任务的方式。您需要使用@hostlistner进行事件侦听。Hostlistner的示例实现

@HostListener('window:orientationchange', ['$event'])
onOrientationChange(event) {
console.log('orientationChanged');

}

相关内容

最新更新