import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root' }) export class DeviceDetectionService { private _isMobile: boolean; constructor() { this._isMobile = this.checkIfMobile(); } isMobile(): boolean { return this._isMobile; } private checkIfMobile(): boolean { const userAgent = navigator.userAgent.toLowerCase(); const isMobileUA = /android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(userAgent); const isTouchDevice = 'ontouchstart' in window || navigator.maxTouchPoints > 0; const isSmallScreen = window.innerWidth < 768; return isMobileUA || (isTouchDevice && isSmallScreen); } recheckDevice(): void { this._isMobile = this.checkIfMobile(); } }