Conceal tablet button with Angular's device detector service

Recently, I've been exploring ways to hide an element specifically on tablet devices using the Angular ngx device detector service.

Although I have successfully achieved this through CSS media queries, I am curious to see if I can accomplish the same result using ngx device detector in my project.

Here is a snippet of the code:

<div id="hide-download">
          <app-button name="download" classAttr="btn-primary" (click)="downloadFile()" [disabled]="!allOrders || allOrders.length === 0">
              <i class="fas fa-download button-icon-padding"></i>
              Download
          </app-button>

In my Angular service file:

@Injectable()
export class ExampleDeviceDetectorService {

  public deviceInfo: any;
  public isMobile: any;
  public isTablet: any;
  public isDesktop: any;

  constructor(
    public deviceService: DeviceDetectorService
  ) {
    this.getDeviceInfo();
  }

  getDeviceInfo() {
    this.deviceInfo = this.deviceService.getDeviceInfo();
    this.isMobile = this.deviceService.isMobile();
    this.isTablet = this.deviceService.isTablet();
    this.isDesktop = this.deviceService.isDesktop();
  }

}

My intention was for the button to be hidden only on tablet devices.

Answer №1

Integrate the service into your component

example.component.ts

constructor(private exampleDeviceDetectorService: ExampleDeviceDetectorService) {}

Utilize *ngIf to eliminate the element from the DOM

example.component.html

<div id="hide-download" *ngIf="!exampleDeviceDetectorService.isTablet">
    <app-button name="download" classAttr="btn-primary" 
    (click)="downloadFile()" [disabled]="!allOrders || allOrders.length === 0">
        <i class="fas fa-download button-icon-padding"></i>
            Download
    </app-button>
</div>

Use [hidden] attribute to conceal the element in the DOM

example.component.html

<div id="hide-download" [hidden]="!exampleDeviceDetectorService.isTablet">
    <app-button name="download" classAttr="btn-primary" 
    (click)="downloadFile()" [disabled]="!allOrders || allOrders.length === 0">
        <i class="fas fa-download button-icon-padding"></i>
            Download
    </app-button>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Angular HTTP post promise is not functioning, whereas AJAX is working effectively

I am encountering an error while attempting to create a post using the code snippet below: short-form.component.ts this.shortFormService.update(formModel) .then(res => { this.router.navigate(['/goHere']) } ); short-for ...

Tips for handling interfaces that are of an unusually large size?

Imagine having an interface like the one below with 10 or more properties: export interface EmployeeFilter { name: string; gender: string; age: number; from: Date; to: Date; module: string; position: string; phone: string; email: string; ...

I am experiencing an issue with applying responsiveFontSize() to the new variants in Material UI Typography

I am looking to enhance the subtitles in MUI Typography by adding new variants using Typescript, as outlined in the documentation here. I have defined these new variants in a file named global.d.ts, alongside other customizations: // global.d.ts import * a ...

I am interested in preserving a QrCode by converting it into an image

Below is the updated code snippet: [HttpGet("GenerateQrCode")] public ActionResult<string> GenerateQrCode(string sellername, string vatregistration, string timestamp, string invoiceamount, string vatamoun) { string ltr = ((char)0x20 ...

Enabling source mapping in Create React App with TypeScript and webpack

After converting my create-react-app to TypeScript, I noticed that I can no longer see the file names in the console when using developer tools. This is what my tsconfig.json looks like: "target": "ES2018", "module": "c ...

The file transfer functionality in object FileTransfert is malfunctioning on certain Android devices when attempting to upload

A mobile application was created to facilitate the sharing of items. Users are required to provide information about the item they are sending, along with the option to add a picture of the object. To achieve this functionality, plugins such as cordova f ...

Using parametric types as type arguments for other types in TypeScript or Flow programming languages

Consider the various types showcased for demonstration: type TranslateToEnglish<A extends string> = A extends "1" ? "one" : A extends "2" ? "two" : A extends "3" ? "three" : "e ...

Issue with Highcharts React: The menu button fails to switch from displaying "view full screen" to "exit full screen" when full screen mode is activated

We've encountered some difficulties while using highcharts in React. Specifically, when switching to full screen mode with the menu button, the option for "Exit from full screen" is not visible. Instead, it still reads "View in full screen." Oddly eno ...

Exploring the inner workings of view encapsulation in Angular

It is common knowledge that the default view encapsulation for a component in an Angular application is Emulated. encapsulation: ViewEncapsulation.Emulated I am quite perplexed about how it functions without being a shadow DOM. ...

Issue encountered while generating a fresh migration in TypeORM with NestJs utilizing Typescript

I am currently working on a Node application using TypeScript and I am attempting to create a new migration following the instructions provided by TypeORM. Initially, I installed the CLI, configured my connection options as outlined here. However, when I ...

Optimizing Your Angular2 Bundle with Webpack: Best Practices for Minimizing Code Size

I have attempted to include the --optimize-minimize option in my webpack command (version 2.1.0-beta.27), but I am still receiving a bundle.js instead of a bundle.min.js: "build:production": "node_modules/.bin/del-cli public/js/app && node_module ...

Acknowledgment Pop-up

When using the PrimeNG table with a custom modal component that I created, I encountered an issue. The edit functionality works correctly and retrieves the correct row id, however, the delete function always returns the id of the first row. dashboard.html ...

Having trouble passing mock data into a functional component and attempting to iterate over an array to display attributes in spans. However, encountering type errors that prevent access to the mock data

I am currently working on a React/TypeScript Component where I need to import mock data in order to iterate over it and display a specific attribute within a span element. However, I have encountered some challenges. It seems that I am unable to pass the ...

Limitations of typescript generics

Sorry for the unclear title, I'm struggling to phrase this question concisely! I'm attempting to create a class: class Stream<Template<T,K,I>> I realize this is not the correct syntax. So far, I have... class Stream<T extends ob ...

Can you explain the distinction between the controls and get methods used with the FormGroup object?

I have encountered an interesting issue with 2 lines of code that essentially achieve the same outcome: this.data.affiliateLinkUrl = this.bookLinkForm.controls['affiliateLinkUrl'].value; this.data.affiliateLinkUrl = this.bookLinkForm.get(' ...

The Node.js application is unable to execute due to the error: "View "error" could not be found in the views directory."

Currently, I am following a tutorial that covers the integration of social login with Passport and Node. You can find the tutorial here: Tutorial Link In line with the tutorial, I have started working on a project while utilizing Windows 10 operating syst ...

Error in Angular: Unable to load resource - server returned a 404 status code on IIS

My Angular-13 application is hosted on IIS under the name: employee-app The URL for accessing it is: https://localhost:8443/employee-app/ To set it up, I ran the command: ng build --base-href "/employee-app" I also included a Web.config fi ...

Mastering unit testing with Behaviour Subjects in Angular

I am looking to test the get and set methods of my user.store.ts file. The get() method is used to retrieve users, while addUsers() is utilized to add new Users to the BehaviorSubject. How can I accomplish this? import { Injectable } from '@angular/c ...

Create a unique NPM package tailored to my needs, available for easy installation across all of my projects

My plan is to create a custom "skin" on top of bootstrap for various UI projects. Instead of manually copying and pasting styles and components from my current Angular 5 project, I want to develop an NPM package that can be easily installed in new projects ...

What is the correct method for specifying the date field in my TypeScript interface that will be retrieved from my cloud function?

Context: Utilizing Vue to interact with cloud functions for accessing data from firestore (avoiding direct firestore queries). A function is called wherein one field returns firestore's timestamp representation (seconds, nanoseconds). Also, I'm a ...