2017-12-14 4 views
1

Ich versuche, eine eckige Eigenschaft mit dem * ngIf zu binden, funktioniert aber überhaupt nicht.Angular NgZone in Marker-Listener

zeige ich Ihnen meinen Code, zuerst meine loadMap Funktion:

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

Und die addMarkers() Funktion:

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); 
     }); 
     }); 
    }); 
    } 

Das Problem dabei ist, sogar innerhalb des ngZone.run(), um diese verweist auf den Marker und nicht auf die Komponente. Ich weiß nicht, wie ich das Problem lösen könnte.

Vielen Dank im Voraus.

Grüße.

Antwort

2

würde ich Pfeil Funktion anstelle von Funktionsausdruck verwendet this

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

Danke zu behalten! das funktioniert jetzt. – Bruno