MrF*_*man 2 typescript ionic-framework ionic2 ionic3 angular
是否可以在TypeScript中为"hold click"事件分配按钮?
喜欢:
<button ion-button icon-only color="royal" (click)="addNote()" (holdclick)="removeNote()">
Run Code Online (Sandbox Code Playgroud)
您可以使用该press事件(手势文档中的更多信息):
import { Component } from '@angular/core';
@Component({
templateUrl: 'template.html'
})
export class BasicPage {
public press: number = 0;
constructor() {}
pressEvent(e) {
this.press++
}
}
Run Code Online (Sandbox Code Playgroud)
在视图中:
<ion-card (press)="pressEvent($event)">
<ion-item>
Pressed: {{press}} times
</ion-item>
</ion-card>
Run Code Online (Sandbox Code Playgroud)
如果这还不够(可能在您的场景中需要更长的新闻事件),您可以通过创建自定义指令来创建自己的手势事件.更多信息可以在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();
}
}
Run Code Online (Sandbox Code Playgroud)
然后在你的html元素中使用它:
<button longPress>...<button>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7119 次 |
| 最近记录: |