首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ngx-传单中单击标记后材料对话框未打开

在ngx-传单中单击标记后材料对话框未打开
EN

Stack Overflow用户
提问于 2020-05-12 07:43:37
回答 1查看 528关注 0票数 0

我用ngx-leafletngx-leaflet-draw来显示传单地图。我可以从工具栏标记图标在地图上显示一个标记。单击标记时,我希望显示“材料对话框”组件。当我单击标记时,我可以在控制台上显示标记坐标。代码是

代码语言:javascript
复制
public onMapReady(map: L.Map) {
    map.on(L.Draw.Event.CREATED, function(e) {
      const type = (e as any).layerType,
        layer = (e as any).layer;

      if (type === 'marker') {
        const markerCoordinates = layer._latlng;
        layer.on('click', () => {
          console.log(markerCoordinates); // works properly
        });
      }
    });
  }

然后,我尝试修改显示材料对话框组件的代码,但得到了错误。

代码语言:javascript
复制
import { NgZone } from '@angular/core';
import { MatDialog, MatDialogRef } from '@angular/material/dialog';
import { MaterialDialogComponent } from './m-dialog.component';
...
...
export class NgxLeafletComponent {
  dialogRef: MatDialogRef<MaterialDialogComponent>;

  public constructor(private zone: NgZone) {}

  public onMapReady(map: L.Map) {
      map.on(L.Draw.Event.CREATED, function(e) {
        const type = (e as any).layerType,
          layer = (e as any).layer;

        if (type === 'marker') {
          const markerCoordinates = layer._latlng;
          layer.on('click', () => {
            console.log(markerCoordinates); 
            this.zone.run(() => this.onClickMarker()); //error
          });
        }
      });
    }

  onClickMarker() {
    this.dialogRef = this.dialog.open(MaterialDialogComponent);
  }
 }

错误输出:

我也尝试不使用zone.run()方法,直接使用dialog.open()方法,但再次捕获错误。

Uncaught :无法读取未定义的属性“打开”

注意:,当我在onMapReady()外部尝试这个方法时,如果没有ngx-leaflet,它就会工作得很好。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-17 14:54:48

我找到问题并解决了它。在这里,我在map.on(L.Draw.Event.CREATED, function(e) {...}上使用了正则函数(),不允许调用另一个函数。因此,它需要箭头函数来调用其中的另一个方法/函数。

代码语言:javascript
复制
import { NgZone } from '@angular/core';
import { MatDialog, MatDialogRef } from '@angular/material/dialog';
import { MaterialDialogComponent } from './m-dialog.component';
...
...
export class NgxLeafletComponent {
  dialogRef: MatDialogRef<MaterialDialogComponent>;

  public constructor(private zone: NgZone) {}

  public onMapReady(map: L.Map) {
      map.on(L.Draw.Event.CREATED, (e) => {
        const type = (e as any).layerType,
          layer = (e as any).layer;

        if (type === 'marker') {
          const markerCoordinates = layer._latlng;
          layer.on('click', () => {
            console.log(markerCoordinates); 
            this.zone.run(() => this.onClickMarker()); //error
          });
        }
      });
    }

  onClickMarker() {
    this.dialogRef = this.dialog.open(MaterialDialogComponent);
  }
 }

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61746311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档