【问题标题】:Angular NgZone in marker listener标记侦听器中的 Angular NgZone
【发布时间】:2018-05-28 09:52:10
【问题描述】:

我正在尝试将一些角度属性与 *ngIf 绑定,但根本不起作用。

我给你看我的代码,首先是我的 loadMap 函数:

  loadMap() {
    this.map = new google.maps.Map(document.getElementById('map'), this.mapOptions);
    google.maps.event.addListenerOnce(this.map, 'idle', ()=>{
      this.addMarkers()
    });
  }

还有 addMarkers() 函数:

addMarkers() {
    var me = this;
    this.restaurants.map( restaurant => {
      new google.maps.Marker({
        position: new google.maps.LatLng(restaurant.location.latitude, restaurant.location.longitude),
        map: this.map,
        animation: google.maps.Animation.DROP,
        name: restaurant.id
      })
      .addListener('click', function() {
        me.ngZone.run(() => {
          this.restaurantSelected = restaurant
          console.log(this.restaurantSelected);
        });
      });
    });
  }

这个问题甚至在 ngZone.run() 内部也是引用标记而不是组件。我不知道我该如何解决这个问题。

提前谢谢你。

问候。

【问题讨论】:

    标签: angular google-maps listener google-maps-markers


    【解决方案1】:

    我会使用箭头函数而不是函数表达式来保留this

    .addListener('click', () => {
                         ^^^^^^^^
       me.ngZone.run(() => {
          this.restaurantSelected = restaurant
          console.log(this.restaurantSelected);
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-01
      • 2023-03-10
      • 1970-01-01
      • 2011-12-04
      • 1970-01-01
      • 1970-01-01
      • 2011-11-03
      • 2014-09-10
      相关资源
      最近更新 更多