Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic Framework hold click event

Is it possible to assign a button to a "hold click" event in TypeScript?

Like:

<button ion-button icon-only color="royal" (click)="addNote()" (holdclick)="removeNote()">
like image 831
MrFlyingToasterman Avatar asked Jan 22 '26 12:01

MrFlyingToasterman


1 Answers

You can use the press event (more info in the Gestures docs):

import { Component } from '@angular/core';

@Component({
  templateUrl: 'template.html'
})
export class BasicPage {

  public press: number = 0;

  constructor() {}

  pressEvent(e) {
    this.press++
  }

}

And in the view:

  <ion-card (press)="pressEvent($event)">
    <ion-item>
      Pressed: {{press}} times
    </ion-item>
  </ion-card>

If that's not enough (maybe you need a longer press event in your scenario), you can create your own gesture event by creating a custom directive. More information can be found in this amazing article by roblouie. The article uses an old version of Ionic, but the main idea is still the same (and pretty much all the code should work like it is):

import {Directive, ElementRef, Input, OnInit, OnDestroy} from '@angular/core';
import {Gesture} from 'ionic-angular';

@Directive({
  selector: '[longPress]'
})
export class PressDirective implements OnInit, OnDestroy {
  el: HTMLElement;
  pressGesture: Gesture;

  constructor(el: ElementRef) {
    this.el = el.nativeElement;
  }

  ngOnInit() {
    this.pressGesture = new Gesture(this.el, {
      recognizers: [
        [Hammer.Press, {time: 6000}] // Should be pressed for 6 seconds
      ]
    });
    this.pressGesture.listen();
    this.pressGesture.on('press', e => {
      // Here you could also emit a value and subscribe to it
      // in the component that hosts the element with the directive
      console.log('pressed!!');
    });
  }

  ngOnDestroy() {
    this.pressGesture.destroy();
  }
}

And then use it in your html element:

<button longPress>...<button>
like image 183
sebaferreras Avatar answered Jan 24 '26 16:01

sebaferreras



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!