【发布时间】:2017-09-15 12:17:36
【问题描述】:
我正在尝试在元素上添加自定义 longPress 事件指令,因为 (press)="callback_function()" 将导致 ion-list 无法再滚动。不管有没有错误,我发现我需要添加一个自定义手势指令来添加对新属性的支持,在这种情况下我称之为 longPress。它工作得很好,除了我不知道如何添加回调函数:-)
我已经学习了一个教程 (http://roblouie.com/article/198/using-gestures-in-the-ionic-2-beta/)
“press-directive”在 src/components/press-directive/press-directive.js 中创建,如下所示:
import { Directive, ElementRef, OnInit, OnDestroy } from '@angular/core';
import { Gesture } from "ionic-angular/gestures/gesture";
/**
* Generated class for the PressDirective directive.
*
* See https://angular.io/docs/ts/latest/api/core/index/DirectiveMetadata-class.html
* for more info on Angular Directives.
*/
@Directive({
selector: '[longPress]' // Attribute selector
})
export class PressDirective implements OnInit, OnDestroy {
el: HTMLElement;
pressGesture: Gesture;
constructor(el: ElementRef) {
this.el = el.nativeElement;
}
public theCallback() {
}
ngOnInit() {
this.pressGesture = new Gesture(this.el);
this.pressGesture.listen();
// instead of this..
this.pressGesture.on('press', (event) => {
console.log('pressed!!');
});
// i want the callback to come from the template like this:
// <ion-col (longPress)="showActionSheet(object)">
}
ngOnDestroy() {
this.pressGesture.destroy();
}
}
在 home.module.ts 中,我在导入中添加了指令:
import { PressDirective } from "../../components/press-directive/press-directive";
我已经在声明中添加了它:
declarations: [
Home,
PressDirective
],
在home.html中,我是这样实现的:
<ion-col (longPress)="showActionSheet(relevantObject)">...
我已经删掉了大部分不重要的东西 :-)
当我长按时,它会返回以下内容:
console.log('pressed!!');
但我不知道如何支持模板元素的实际回调函数。
任何帮助或提示将不胜感激..
【问题讨论】:
标签: angular ionic-framework ionic2