There seems to be an issue with the touchStart and touchEnd events in Angular2 when viewed on mobile devices

The hover property in CSS is used to create interactive effects, but when trying to apply similar functionality on mobile devices using touchstart and touchend events, I am encountering issues with the responsiveness of the events.

Below are the implementations of both touch events:

@HostListener('touchstart', ['$event']) onTouchStart(event: Event): void {
    if (event) {
      event.preventDefault();
    }
    let part = this.el.nativeElement.querySelector('.icon-interface-question-mark');
    this.renderer.setElementStyle(part, 'color', 'green');
  }



@HostListener('touchend', ['$event']) onTouchEnd(event: Event): void {
    if (event) {
      event.preventDefault();
    }
    let part = this.el.nativeElement.querySelector('.icon-interface-question-mark');
    this.renderer.setElementStyle(part, 'display', 'yellow');
}

Here is the relevant CSS code:

.account-outlook-wrapper {
  .icon-interface-question-mark {
    color: yellow;
    position: relative;
    cursor: pointer;

    &:hover {
      color: green;
    }
  }

I made a modification to the CSS for testing purposes:

.account-outlook-wrapper {
  .icon-interface-question-mark {
    color: orange;
    position: relative;
    cursor: pointer;

    &:hover {
      color: red;
    }
  }

However, despite the changes in the CSS, the color remained green in a window browser due to the touchstart event. It was supposed to work only for mobile views. Please advise me on how to properly utilize both touch events.

Answer №1

touch events work perfectly.

There are some other issues aside from CSS or events:

  1. The Renderer2 does not have a method called setElementStyle. Please use setStyle
  2. You are attempting to set the color yellow as the value of display, but display does not accept colors

Check out this example: https://stackblitz.com/edit/angular-s5wfhf

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

Guide to asynchronously loading images with Bearer Authorization in Angular 2 using NPM

I am in search of a recent solution that utilizes Angular2 for my image list. In the template, I have the following: <div *ngFor="let myImg of myImages"> <img src="{{myImg}}" /> </div> The images are stored as an array w ...

In JavaScript, a variable's value can be assigned to another variable using the variable

I have a collection of predetermined variables. I dynamically assign a value to another variable, which ends up representing the name of one of the predefined variables. This allows me to easily determine which set variable to use on a particular section o ...

Send your data to Node.js in tabular form

I am facing an issue with sending an editable table to my Node.js server. The response I am getting is "undefined". How can I resolve this problem? All the elements of my form are being parsed successfully, except for the table. The server logs the follo ...

`How can I enhance the appearance of an Angular 4 component using an attribute?`

There is a component where I need to pass specific data to the child components within an ngFor loop using attributes. Depending on the attribute, I want to style these child components accordingly. Code testimonials.component.html - (Parent component) ...

Ways to retrieve the value of a variable beyond its scope while using snapshot.foreach

I am experiencing an issue where the return statement returns a null value outside the foreach loop of the variable. I understand that the foreach loop creates its own scope, but I need to figure out how to return the value properly... this.selectedUserMe ...

Adjusting Google Maps API v3 Autocomplete dropdown width with JavaScript and SASS to match input field dimensions

I am facing an issue where the autocomplete dropdown (div with class "pac-container") is consistently 4 pixels shy of aligning perfectly with the right side of the input field. It looks like this: https://i.sstatic.net/Y0oq1.png Below is the HTML code: ...

Issue Establishing Connection Between Skill and Virtual Assistant Via Botskills Connect

Encountering errors while connecting a sample skill to a virtual assistant. Both are in typescript and function individually, but when using botskills connect, the following errors occur: Initially, ran botskills connect with the --localManifest parameter ...

Guide to transmitting a boolean value from a service to a component using observables in Angular

How can I pass a boolean value from a service to a component? This boolean switches between true and false in two functions: onStartBlockUI and onStopBlockUI. In onStartBlockUI, the boolean is set to true, while in onStopBlockUI, it is set to false. I need ...

In JavaScript, learn how to trigger a statement only when two specific events occur simultaneously

<html> <head> <style> div{ border: 1px solid black; width: 500px; height: 500px; } </style> <script> window.onload = function(){ document.body.onmousedown = function ...

What is the best way to save <div> tags to localStorage?

Currently, I am facing an issue with setting an array of items (divs) to Local Storage. My intention is for these elements to be displayed in the Ui when the page reloads. The function I have written checks if there is already an array stored with that nam ...

Framer Motion does not support animations for exiting elements

Why is it that when I specify the exit property of framer motion to my HTML elements, they fail to animate upon being removed from the DOM? Here's an example of what my code looks like: <motion.div initial={{opacity: 0, y: -500}} animate={ ...

Discovering the art of importing JavaScript files dynamically within controllers

I have one main form through which I pass data from 10 different components, each including the ID of a table that I need to retrieve data from in the database. The issue I am facing is that the code responsible for fetching this data asynchronously is spr ...

Using hooks for conditional rendering

Background Information : My current project involves creating a form using hooks and rendering components based on the step. Challenge Encountered : I received this error message: "Error: UserFormHooks(...): Nothing was returned from render. This usually ...

Adjust the field's color depending on the outcome displayed within

I'm trying to implement a feature where the field value changes to green when "OK" is selected and red when "NOK" is chosen. I have written the following JavaScript code but it's not working as expected - the colors change before clicking submit, ...

The custom pattern validation for Formly Email is malfunctioning

Is there a way to validate email input using ngx-formly? I attempted the code below but it didn't work as expected app.module.ts export function EmailValidator(control: FormControl): ValidationErrors { return /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a ...

Struggling to toggle the visibility of a table with a button - successfully hiding it, but unable to make it reappear?

I need a button that can toggle (show/hide) a table. Currently, my code hides the table successfully, but it fails to show the table again when I click the button. It seems like there is an issue with refreshing or redirecting after clicking the button for ...

How can I stretch a background image using jquery to cover the entire document instead of just the window

I have implemented a plugin called https://github.com/srobbin/jquery-backstretch to effectively stretch the background image. The problem arises when the content of the page is long enough to create a scrollbar. In this case, while scrolling, the backgrou ...

Issue with Socket.Io causing transport polling error

Every time I attempt to use socket.io v1.3.2, I am encountering a transport error. This issue is merely a trial run with socket.io to enhance my understanding of its functionality. The code snippet below is extracted from the official socket.io documentat ...

Converting a cast method into a function in Typescript

With some experimenting on WebRTC, I found that the ondatachannel callback takes a function, but I'm wondering if it's possible to use a method of a Typescript class instead. Can someone please assist me with this? export class MainComponent imp ...

Is it possible to execute node.js code within a Java script environment?

I have a javascript code that functions as a discord bot. Typically, to run this bot, I would open the command prompt in the js path and enter node {file_name}.js. Now, let's say I have another javascript code that I want to execute on button click an ...