1

I've just started programming with angular and I apologize if the question is simple or asked badly.

I have a custom directive LongPressDirective.ts

import { Directive, ElementRef, OnInit, OnDestroy ,Output , EventEmitter } 
from '@angular/core';
import { Gesture } from "ionic-angular/gestures/gesture";
declare let Hammer: any


@Directive({
 selector: '[long-press]' // Attribute selector

})
export class LongPressDirective implements OnInit, OnDestroy{
el: HTMLElement;
  pressGesture: Gesture;
@Output('long-press') onPressRelease: EventEmitter<any> = new 
EventEmitter();
//Hammer: any

 constructor(el: ElementRef) {
this.el = el.nativeElement;
console.log('Hello LongPressDirective Directive'); 
}

ngOnInit() {
//this.pressGesture = new Gesture(this.el);
this.pressGesture = new Gesture(this.el, {
recognizers: [
  [Hammer.Press, {time: 1000}]

]
 });
this.pressGesture.listen();

this.pressGesture.on('press', (event) => {
  console.log('pressed!!');
  this.onPressRelease.emit('released');
});


}

ngOnDestroy() {
this.pressGesture.destroy();
}
}

i have import this directive in app.module.ts

in my HTML use this in same mode:

<div #detailB *ngIf = "condition" class = "imgDetailBlur" [ngStyle]=" 
{'background-image': 'url('path')'}" id="detailB" (long-press)="clearImage()" >

but non working the long-press have try to import che directive in app.module.ts and in the single module but non working

Matteo
  • 1,241
  • 2
  • 21
  • 28
  • I don't know much about ionnic, perhaps take a look to https://stackoverflow.com/questions/43492833/ionic-2-setting-callback-for-long-press-event-directive – Eliseo Jun 04 '18 at 16:09
  • 1
    https://www.npmjs.com/package/ionic-long-press i tested, it works – mohsen solhnia Jun 04 '18 at 17:30
  • Here's an example of ionic-long-press integration https://github.com/nioperas06/30-ionic-projects-in-30-days/tree/master/03.SelectItems – Junior Gantin Jun 04 '18 at 17:45

1 Answers1

1

I solved in this mode:

create a new module SharedModule.ts

import { NgModule } from '@angular/core';
import { LongPressDirective } from '../directives/long-press/long-press';

@NgModule({
   declarations: [
   LongPressDirective
  ],
exports: [
   LongPressDirective
  ]
})
export class SharedModule {

 }

In this module i declared and import my custom directive "LongPressDirective"

Now use SharedModule in the other modules and work!

Matteo
  • 1,241
  • 2
  • 21
  • 28