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

364
Vistas
How to get Component reference(this) in D3 Context Menu OnOpen and OnClose

I'm new to Angular and trying to add d3-context-menu in my Angular project. I successfully added Context Menu when user do right-click.

this.svg = d3.select(this.elementRef.nativeElement)
  .append('div')
  //.on('click', (d, this) => this.onClickTask(d3.event,taskData, this))
  .on('contextmenu', contextMenu(this.menuItems, {
    onOpen: () => {
      // const status = this.MycomponentVariable;   // issue here to get "this" of my component
      console.log('Menu has been OPENED.');
    },
    onClose: () => {
      // const status = this.MycomponentVariable;    // issue here to get "this" of my component
      console.log('Menu has been closed.');
    }
  }));

But the problem I facing that I want a variable (this.MycomponentVariable) value of my angular component via this(component context) when contxt-menu is opened or closed. But it shows that this is undefined. So, I'm unable to get value of this.MycomponentVariable.

I read documentation of d3-context-menu over this-link but couldn't get any solution. Can anyone help me out that how can I get my angular component context inside onOpen and onClose methods?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Really use d3 (or jQuery) is not a great idea, but I imagine you can always create and dispach a CustomEvent.

this.svg = d3.select(this.elementRef.nativeElement)
  .append('div')
  .on('contextmenu', contextMenu(this.menuItems, {
    onOpen: () => {
      document.dispatchEvent(new CustomEvent('menu',{detail:'Open'}}));
    },
    onClose: () => {
      document.dispatchEvent(new CustomEvent('menu',{detail:'Close'}}));
    }
  }));

And use rxjs fromEvent operator

  ngOnInit() {
    this.menuObs=fromEvent(document,'menu').subscribe((res:any)=>{
        console.log(res.detail)
    })

  ngOnDestroy(){
    this.menuObs.unsubscribe()
  }
about 4 years ago · Juan Pablo Isaza 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