【问题标题】:Ionic Framework hold click eventIonic Framework 按住点击事件
【发布时间】:2017-11-29 06:51:57
【问题描述】:

是否可以在 TypeScript 中将按钮分配给“按住单击”事件?

喜欢:

<button ion-button icon-only color="royal" (click)="addNote()" (holdclick)="removeNote()">

【问题讨论】:

    标签: angular typescript ionic-framework ionic2 ionic3


    【解决方案1】:

    您可以使用press 事件(Gestures docs 中的更多信息):

    import { Component } from '@angular/core';
    
    @Component({
      templateUrl: 'template.html'
    })
    export class BasicPage {
    
      public press: number = 0;
    
      constructor() {}
    
      pressEvent(e) {
        this.press++
      }
    
    }
    

    在视图中:

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

    如果这还不够(也许您的场景中需要更长的按下事件),您可以通过创建自定义指令来创建自己的手势事件。更多信息可以在this amazing article by roblouie找到。这篇文章使用了旧版本的 Ionic,但主要思想仍然是相同的(几乎所有代码都应该像现在这样工作):

    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();
      }
    }
    

    然后在你的 html 元素中使用它:

    <button longPress>...<button>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 2018-01-10
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多