有没有办法在Angular 4中访问这个内部子函数的引用?

时间:2018-02-28 01:41:13

标签: angular

我在这里提到的组件中有方法。

export class MapComponent implements OnInit {

  map: any;
  dataLayer: any;
  infoBoxLayer: any;
  infobox: any;
  infoBoxCollection = [];

LoadMapView() {
    var that = this;

    var location = new Microsoft.Maps.Location(40.0583, -74.4057);

    this.map = new Microsoft.Maps.Map(document.getElementById('myMap'), {
      credentials: this.config.get('BingMapKey'),
      center: location,
      mapTypeId: Microsoft.Maps.MapTypeId.road,
      zoom: this.lastZoomLevel,
      liteMode: true,
      enableClickableLogo: false,
      showLogo: false,
      showTermsLink: false,
      showMapTypeSelector: false,
      showTrafficButton: true,
      enableSearchLogo: false,
      showCopyright: false
    });

    // Create a layer to load pushpins to.
    this.dataLayer = new Microsoft.Maps.EntityCollection();
    this.infoBoxLayer = new Microsoft.Maps.EntityCollection();
    this.map.entities.push(this.infoBoxLayer);

    var metadata = {
      Id: '12345',
      title: 'testing'
    }

    NewPin = new Microsoft.Maps.Pushpin(location, { icon: '../icon/pin.png' });

    this.map.entities.push(NewPin);

    this.dataLayer.push(NewPin);

    Microsoft.Maps.Events.addHandler(NewPin, 'click', pushpinClicked);

    this.infobox = new Microsoft.Maps.Infobox(maps.getCenter(), {
        visible: false
    });

    function pushpinClicked(e) {

      if (e.target.metadata) {
        that.infobox.setMap(maps);
        that.infobox.setOptions({
          location: e.target.getLocation(),
          visible: true,
          showCloseButton: true,
          offset: new Microsoft.Maps.Point(0, 20),
          htmlContent: '<div class = "infy" style= "background-color: white;border: 1px solid lightgray; width:520px;">'
            + e.target.metadata + '</div>'
        });
        // push this infoBox
        that.infoBoxCollection.push(that.infobox);
        }
    }
}}

我已分配&#34; &#34;参考&#34; &#34; LoadMapView()中的变量因为我无法使用此引用在 pushpinClicked 函数中获取局部变量。

无论如何要避免&#34; &#34;变量来访问此变量的引用?

由于&#34; &#34;我面临内存泄漏问题变量&#34;

1 个答案:

答案 0 :(得分:1)

更改

function pushpinClicked(e) {...

...到...

const pushpinClicked = (e) => {...