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

158
Vistas
Passing data inside click method to containing angular component

I have a Angular component "MapComponent" where I use Javascript inside ngAfterViewInit() function to initialize the Leaflet map. I also bind a onClick function to map component. My goal is to log a MapComponent member to the console when the map is clicked. When I the onMapClick function is triggered by the click event on the map, this is interpreted as the map but not the MapComponent. How can I achieve to get access to the MapComponent members inside onMapClick function?

import { Component, OnInit } from '@angular/core';
import { LeafletMouseEvent, Map } from 'leaflet';
declare let L: any;

@Component({
  selector: 'app-map',
  templateUrl: './map.component.html',
  styleUrls: ['./map.component.css']
})
export class MapComponent implements OnInit {

  private name = 'Test';
  private view = [52, 9];
  private start = [52.373920, 9.735603];
  private end = [52.021111, 8.534722];

  constructor() { 
    
  }


  onMapClick(e: LeafletMouseEvent) {
    console.log( this.name );
  }

  


  ngAfterViewInit(){
    
    // Creating map element
    const map = L.map('map', {tap: false, zoomControl: false}).setView( this.view, 13);

    // Creating tile layer
    const tiles = L.tileLayer('https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png', {
      attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
    });

    // Adding tile layer to map
    tiles.addTo(map);
  

    map.fitBounds([
      this.start,
      this.end
  ]);

    
  map.on('click', this.onMapClick);

  };


  

  // ngOnInit 
  ngOnInit( ): void {

  }
}
about 4 years ago · Juan Pablo Isaza
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