Ionic2 Ionic/Floaple.js:如何从L.Draw.Event.CREATED事件调用函数?

Ionic2 Ionic/Floaple.js:如何从L.Draw.Event.CREATED事件调用函数?,ionic2,leaflet.draw,Ionic2,Leaflet.draw,我正在使用Leaftlet Draw在地图上绘制简单的形状。创建形状时,我想调用另一个方法 我正在听CREATE这样的活动: drawMap() { this.myMap = L.map('map', { crs: L.CRS.Simple }); ... this.myMap.on(L.Draw.Event.CREATED, function (e) { let type = e.layerType; let laye

我正在使用Leaftlet Draw在地图上绘制简单的形状。创建形状时,我想调用另一个方法

我正在听
CREATE
这样的活动:

drawMap() {
    this.myMap = L.map('map', {
        crs: L.CRS.Simple
    });

    ...

    this.myMap.on(L.Draw.Event.CREATED, function (e) {
      let type = e.layerType;
      let layer = e.layer;

      this.myOtherMethod();  // this.myOtherMethod is not a function

      drawLayer.addLayer(layer);
    });
}

myOtherMethod() {
    console.log('hello world!');
}
如果我使用
这个.myOtherMethod()在事件监听器之外,它会称之为fine,所以我知道这是一个范围问题。我不知道如何调用父作用域


谢谢你的建议

不要使用匿名函数作为回调函数,而是创建一个单独的命名函数,然后可以调用
myOtherMethod

drawMap() {
    this.myMap = L.map('map', {
        crs: L.CRS.Simple
    });

    ...

    this.myMap.on(L.Draw.Event.CREATED,this.onCreate.bind(this) );
}

onCreate (e) {
      let type = e.layerType;
      let layer = e.layer;

      this.myOtherMethod();  // this.myOtherMethod is not a function

      drawLayer.addLayer(layer);
}


myOtherMethod() {
    console.log('hello world!');
}

不要使用匿名函数作为回调函数,而是创建一个单独的命名函数,然后调用
myOtherMethod

drawMap() {
    this.myMap = L.map('map', {
        crs: L.CRS.Simple
    });

    ...

    this.myMap.on(L.Draw.Event.CREATED,this.onCreate.bind(this) );
}

onCreate (e) {
      let type = e.layerType;
      let layer = e.layer;

      this.myOtherMethod();  // this.myOtherMethod is not a function

      drawLayer.addLayer(layer);
}


myOtherMethod() {
    console.log('hello world!');
}

先生,你太棒了。非常感谢你!先生,你太棒了。非常感谢你!