角度-this.function不是函数 [英] Angular - this.function is not a function

查看:59
本文介绍了角度-this.function不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对此我有些绝望.我有一个获取数据并使用此数据的信息呈现地图的组件.一切都很好.我在此地图上放置了标记,我想在标记中执行点击功能.这些函数调用组件中定义的另一个函数,并带给我数据以模态形式显示它.但是,当我单击标记时,会收到此错误:

I'm a bit desperate with this. I have a component that takes data and renders a map with the information of this data. All fine here. I put markers on this map and I want to do a click function in the marker. Those function call another function defined in the component and bring me data to show it in a modal. But, when I click the marker, I receive this error:

this.getInfoAlteracion不是函数

this.getInfoAlteracion is not a function

我不知道为什么.我证明了很多事情,但看不到错误.这是我的代码:

I don't know why. I prove a lot of things but I can't see the error. Here's my code:

import { Component, OnInit } from '@angular/core';
import * as L from 'leaflet';
import 'leaflet.markercluster';
import { MapService } from './services';
import * as global from 'app/globals';
import { Alteracion } from './dto/alteracion';

@Component({
  selector: 'map',
  templateUrl: './map.component.html',
  styleUrls: ['./map.component.scss']
})
export class MapComponent implements OnInit {
  public alteraciones: Alteracion[] = [];
  public alteracion: Alteracion[] = [];

  constructor(private mapService: MapService) {}

  ngOnInit() {
    this.getAlteraciones();
  }


  getAlteraciones() {
    this.mapService.getListAlteraciones(11, 1).subscribe(
      result => {
        this.alteraciones = result;
        this.renderMap();
      },
      err => console.log(err)
    );
  }

  getInfoAlteracion(id_alteracion: string) {
    this.mapService.getInfoAlteracion(id_alteracion).subscribe(
      result => {
        this.alteracion = result;
        console.log(this.alteracion);
      },
      err => console.log(err)
    );
  }

  renderMap() {
    L.Icon.Default.imagePath = 'assets/img/theme/vendor/leaflet/';
    let map: any;

      map = L.map('map', {
            zoomControl: false,
            format: 'image/jpeg',
            center: L.latLng(40.4166395, -3.7046087),
            zoom: 2,
            minZoom: 0,
            maxZoom: 19,
            layers: [this.mapService.baseMaps.Esri]});

      L.control.zoom({ position: 'topleft' }).addTo(map);
      L.control.layers(this.mapService.baseMaps).addTo(map);

    let cluster = L.markerClusterGroup();

    for (let al of this.alteraciones) {
      let marker = L.marker([al.coory, al.coorx]);

      marker.on('click', function(e) {
        alert('Id alteración: ' + al.id_alteracion); // THIS WORKS
        this.getInfoAlteracion(al.id_alteracion); // THIS DON'T WORK
        console.log(this.alteracion);
      });

      cluster.addLayer(marker);
    }
    map.addLayer(cluster);   

}

有什么想法吗?预先感谢!

Any ideas? Thanks in advance!

推荐答案

您需要为事件处理程序使用箭头功能

You need to use an arrow function for your event handler

  marker.on('click', (e) => {
    alert('Id alteración: ' + al.id_alteracion); // THIS WORKS
    this.getInfoAlteracion(al.id_alteracion); // THIS DON'T WORK
    console.log(this.alteracion);
  });

在Javascript&打字稿this由呼叫者为function确定.箭头函数从声明站点捕获this. this完全不是您期望的那样.

In Javascript & Typescript this is determined by the caller for a function. Arrow functions capture this from the declaration site. Quite simply this way not what you expected it to be.

这篇关于角度-this.function不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆