Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda