I am trying to add a custom longPress event directive on elements, since (press)="callback_function()" will result in ion-list won't be able to scroll anymore. Bug or not, i've found out that I need to add a custom gesture directive that would add support for a new attribute, in this case I call it longPress. and it works great, except I don't get how to add the callback function :-)
I've followed a tutorial (http://roblouie.com/article/198/using-gestures-in-the-ionic-2-beta/)
The "press-directive" is created in src/components/press-directive/press-directive.js and looks like this:
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();
}
}
In home.module.ts I have added the directive in an import:
import { PressDirective } from "../../components/press-directive/press-directive";
and I've added it in the declaration:
declarations: [
Home,
PressDirective
],
In home.html, I implement it in a like this:
<ion-col (longPress)="showActionSheet(relevantObject)">...
I've cut out most of the unimportant stuff :-)
And when I do a long press, it will return the following:
console.log('pressed!!');
But I can't get my head wrapped around how to support the actual callback function from the template element.
Any help or hint would be appreciated..