Press event is not fired on Ionic 5

Viewed 2532

I want to implement multiple select on multiple items (a list of ion-item-sliding) when one of them is pressed for few seconds by adding (press) to each ion-item-sliding and it seems that the press event is never fired. I moved the press event to ion-item but nothing still happened. Then I tried to test directly the press event on a ion-button and found out that the press event is the problem because when I change it to click it's working perfectly. Here is what I tried:

<ion-button (press)="itemPressed()">Test</ion-button>
itemPressed() {
    console.log("expecting to work");
}
5 Answers

In addition to @Arj 1411's answer add HammerModule in '@angular/platform-browser' imports and this should be work.

I think you can use the tap and click event as mentioned below instead of using press.

<ion-button (click)="itemPressed()">Test</ion-button> 

or

<ion-button tappable (tap)="itemPressed()">Test</ion-button> 

if you want to use the long press event. there is NPM plugin avilable on below url you can use that.. this could help you.

https://www.npmjs.com/package/ionic-long-press

Ionic long press event on cards

Hi you can implement long press with Hammer.js

1.install Hammer.js from npm install hammerjs — save

2.on src/main.ts file make changes like following

 import { enableProdMode } from '@angular/core';
    import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
    import { AppModule } from './app/app.module';
    import { environment } from './environments/environment';
    **import "hammerjs";** 
    
    if (environment.production) {
      enableProdMode();
    }
platformBrowserDynamic().bootstrapModule(AppModule)   .catch(err => console.log(err));

3.on your template and component file

<ion-button (press)="onPress($event)" (pressup)="onPressUp($event)"> Test </ion-button>

onPress($event) {
    console.log("onPress", $event);
}

onPressUp($event) {
    console.log("onPressUp", $event);
}

Try this. If this is not working as expected let me know. I will edit my answer

The best solution is actually to use the contextmenu event instead of press. This is natively supported and so will not need HammerJS (or anything else).

Related