Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!