Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 dblclick event not working in mobile view

I'm using double click functionality in my code. It's working fine in desktop view but the problem is that when I switch to mobile/tablet view double tap doesn't work.

Here's my code sample

HTML:

<a (dblclick)="viewRecord(item.id)">View Record Details</a>

Component:

viewRecord(id) {
  this.router.navigate(['course/view/', id]);
}

Any suggestions on how to resolve this problem will be highly appreciated

like image 393
Tauqeer H. Avatar asked Jun 08 '18 02:06

Tauqeer H.


People also ask

Does Dblclick work on mobile?

Usually, "double tap" is not implemented on mobiles like on Desktop... "activated" on single tap. not rerquire sensitivy, since the second tap can occur at amy time. will not feel it familiar on mobile.

How does angular detect Doubleclick?

The ng-dblclick directive tells AngularJS what to do when an HTML element is double-clicked. The ng-dblclick directive from AngularJS will not override the element's original ondblclick event, both are executed.

How to trigger double click event in jQuery?

The dblclick() is an inbuilt method in jQuery which is used to trigger the double-click event to occur. This method occurs when the selected element will be double clicked. Syntax: $(selector).

What is the use of NG-Dblclick?

The ng-dblclick Directive in AngluarJS is used to apply custom behavior when an element is clicked double. It can be used to show or hide some element or it can popup an alert or change the color of text when it is clicked twice.


1 Answers

That is because, there is no dblclick event registered for mobile devices.

But there is a work around for this. It is kind of a hack. Reference

Instead of listening dblclick you can listen for normal click event

<a (click)="viewRecord(item.id)">View Record Details</a>

component file

private  touchTime = 0;

viewRecord(id) {

    if (this.touchtime == 0) {
        // set first click
        this.touchtime = new Date().getTime();
    } else {
        // compare first click to this click and see if they occurred within double click threshold
        if (((new Date().getTime()) - this.touchtime) < 800) {
            // double click occurred
             this.router.navigate(['course/view/', id]); 
            this.touchtime = 0;
        } else {
            // not a double click so set as a new first click
            this.touchtime = new Date().getTime();
        }
    }
  }

DEMO

like image 166
Amit Chigadani Avatar answered Sep 19 '22 00:09

Amit Chigadani