Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

366
Vistas
How to pass the dom element clicked in angular2/4?

I have the following directive:

import {Directive, HostListener, Output, EventEmitter} from '@angular/core';

@Directive({
  selector: '[long-press]'
})
export class LongPressDirective {
  private touchTimeout: any;
  @Output() longpress = new EventEmitter();

  private rootPage: any;

  constructor() {}

  @HostListener('touchstart') touchstart():void {
    this.touchTimeout = setTimeout(() => {
        this.longpress.emit({});
    }, 400);
  }

  @HostListener('touchend') touchend():void {
      this.touchEnd();
  }
  @HostListener('touchcancel') touchcancel():void {
      this.touchEnd();
  }

  private touchEnd():void {
    clearTimeout(this.touchTimeout);
  }
}

I use this directive like this:

//<ion-item (click)="open(item)" long-press (longpress)="select(item)"></ion-item>

The problem is, I would like to pass the actual reference to the dom element. Usually with a (click) directive, I can do something like: (click)="somefunc($event)". With my directive, I want to do (longpress)="select($event, item)". I need this so that I can add an attribute to the dom. (contenteditable). How do I modify my long-press directive to pass the $event in just like I can already do with (click) out of the box?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can inject $event into HostListener like that:

@HostListener('touchstart', ['$event']) {...}

here is the plunk: https://plnkr.co/edit/j852SpnyorLObg07qmo0

import {Component, NgModule, HostListener, Directive, EventEmitter, Output} from '@angular/core' import {BrowserModule} from '@angular/platform-browser'

@Directive({
  selector: '[myDir]'
})
export class MyDir {
  @Output() myDir = new EventEmitter<any>();

  @HostListener('click', ['$event']) onClick($event) {
    this.myDir.emit($event);
  }
}

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2 (myDir)="clicked($event)">hello</h2>
    </div>
  `,
})
export class App {
  constructor() {
  }

  clicked($event) {
    console.log($event);
  }
}

@NgModule({
  imports: [BrowserModule],
  declarations: [App, MyDir],
  bootstrap: [App]
})
export class AppModule {
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda