Double Tap/ double click Angular2 & ionic
Solution 1
So after 1-2 hours it was obvious, you don't need to catch double click events with Ionic, but with pure JavaScript: dblclick()
So in Angular 2 it would be: (dblclick)="myFunction()"
and that's it!
Here you will find other events for JavaScript.
Solution 2
html file
<button (tap)="tapEvent()">Tap Me!</button>
ts file
let count : number = 0;
tapEvent(){
this.count++;
setTimeout(() => {
if (this.count == 1) {
this.count = 0;
alert('Single Tap');
}if(this.count > 1){
this.count = 0;
alert('Double Tap');
}
}, 250);
}
Solution 3
To catch the double click event, the following can be used:
(dblclick)="clickFunction()"
If we want to fire a function on click and onother function on double click we can use the following:
<button (click)="simpleClickFunction()" (dblclick)="doubleClickFunction()">click me!</button>
However, the simpleClickFunction
function will be called also when doubleClickFunction
is fired. To prevent it to happen, setTimeout
can help as the following:
html template
<button (click)="simpleClickFunction()" (dblclick)="doubleClickFunction()">click me!</button>
Component
simpleClickFunction(): void{
this.timer = 0;
this.preventSimpleClick = false;
let delay = 200;
this.timer = setTimeout(() => {
if(!this.preventSimpleClick){
//whatever you want with simple click go here
console.log("simple click");
}
}, delay);
}
doubleClickFunction(): void{
this.preventSimpleClick = true;
clearTimeout(this.timer);
//whatever you want with double click go here
console.log("double click");
}
Related videos on Youtube
luiswill
Updated on July 05, 2022Comments
-
luiswill over 1 year
I am searching on many forums and questions, but nobody seems to ask how to double click ou double tap in Angular/ionic 2 ?
In ionic v1 it was available with
on-double-tap
(see http://ionicframework.com/docs/api/directive/onDoubleTap/)Does anyone maybe have a tip or any code to catch double click events on ionic 2 / angular 2?
Maybe through HammerJS?
Thank you very much ! Luis :)
-
edkeveked almost 5 yearsCould the one who downvoted the question please explain why ?
-
Edoardoo over 4 yearsI'm not the one who downvoted, but I guess it's because your answer doesn't add anything to the other answers and also because it's not addressing the major issue of multi platform compatibility. But again, it's just a guess.
-
edkeveked over 4 yearsThe answer has been upvoted by others. So the answer is useful for others
-
flyer about 3 yearsThe link to gestures is now here: ionicframework.com/docs/utilities/gestures