我已经为长按创建了一个离子5指令。以下是代码。
export class LongPressDirective implements AfterViewInit {
private delay = 800;
@Output() press = new EventEmitter();
action: any;
private longPressActive = false;
constructor(private el: ElementRef,
private gestureCtrl: GestureController,
private zone: NgZone) { }
ngAfterViewInit() {
this.loadLongPressOnElement();
}
loadLongPressOnElement() {
const gesture = this.gestureCtrl.create({
el: this.el.nativeElement,
threshold: 0,
gestureName: 'long-press',
onStart: ev => {
this.longPressActive = true;
this.longPressAction();
},
onEnd: ev => {
this.longPressActive = false;
}
});
gesture.enable(true);
}
private longPressAction() {
if (this.action) {
clearInterval(this.action);
}
this.action = setTimeout(() => {
this.zone.run(() => {
if (this.longPressActive === true) {
this.longPressActive = false;
this.press.emit();
}
});
}, this.delay);
}
}当我在按钮中使用它并传递Popover的事件时,事件总是未定义的。因此,我的弹出页面出现在屏幕的中间,而不是按钮位置。
<ion-button appLongPress (press)="onPress($event)">
Test
</ion-button>
async onPress(ev: any) {
// ev is undefined here
// raise popover here
}我想我需要在指令的this.press.emit();方法中传递事件,但是我不知道要传递什么。我尝试了几种方法,但都不起作用。
发布于 2021-07-21 21:03:10
只需在此处传递事件
onStart: ev => {
this.longPressActive = true;
this.longPressAction(ev);
},还有这里
private longPressAction(ev) {
...
this.press.emit(ev);
...
}https://stackoverflow.com/questions/68469492
复制相似问题