Search code examples
typescriptangularangular2-changedetection

Angular2 - after SebmGoogleMapMarker event "markerClick" triggers, view is not updating


I have an array which looks like this:

    locations: marker[] = [
    {id: '1',  lat: 51.5239935252832,    lng:  5.137663903579778,   content: 'Kids Jungalow (5p)', iconUrl: 'img/marker.png'},
    {id: '2',  lat: 51.523853342911906,  lng:  5.1377765563584035,  content: 'Kids Jungalow (5p)', iconUrl: 'img/marker2.png'},
    {id: '3',  lat: 51.5237298485607,    lng:  5.137969675407476,   content: 'Kids Jungalow (5p)', iconUrl: 'img/marker2.png'},
    {id: '4',  lat: 51.52355628836575,   lng:  5.138066234932012,   content: 'Kids Jungalow (5p)', iconUrl: 'img/marker2.png'},
    {id: '5',  lat: 51.52340275379578,   lng:  5.138211074218816,   content: 'Kids Jungalow (5p)', iconUrl: 'img/marker2.png'},
    {id: '6',  lat: 51.523199152806626,  lng:  5.138382735595769,   content: 'Kids Jungalow (5p)', iconUrl: 'img/marker2.png' },
    {id: '7',  lat: 51.5229955509073,    lng:  5.138511481628484,   content: 'Kids Jungalow (5p)', iconUrl: 'img/marker2.png'},
    {id: '8',  lat: 51.52280529912936,   lng:  5.138543668136663,   content: 'Kids Jungalow (5p)', iconUrl: 'img/marker2.png'},
    {id: '9',  lat: 51.523596340777075,  lng:  5.138463201866216,   content: 'Kids Jungalow (5p)', iconUrl: 'img/marker2.png'},
    {id: '700',lat: 51.523372714362736,  lng:  5.1386992362595265,  content: 'Kids Jungalow (5p)', iconUrl: 'img/marker2.png'},
    {id: '101', lat: 51.52329594683302,  lng:  5.138838711128301,   content: 'Kids Jungalow Giraffe', iconUrl: 'img/marker2.png'}
];

If I click on a marker it opens a section with the information of the selected marker.

Here the html of the googlemaps:

<sebm-google-map-marker *ngFor="let location of locations" (markerClick)="updateDiv(location)"></sebm-google-map-marker>

Here the function updateDiv():

updateDiv(location: Location) {
    this.selectedLocation = location;
}

Here is the markup of the div where I want to display the id of locations

<div *ngIf="selectedLocation" class="result-number">{{ selectedLocation.id }}</div>

But it seems not to work!


Solution

  • You could try to explicitly call the detectChanges method of the ChangeDetectorRef class:

    constructor(private cdr:ChangeDetectorRef) {
    }
    
    (...)
    
    updateDiv(location: Location) {
      this.selectedLocation = location;
      this.cdr.detectChanges();
    }
    

    First check that the method is actually called with the right location and the selectedLocation is updated within the TypeScript class.

    If this doesn't work, you can leverage NgZone:

    constructor(private ngZone:NgZone) {
    }
    
    (...)
    
    updateDiv(location: Location) {
      this.ngZone.run(() => {
        this.selectedLocation = location;
      });
    }
    

    Processing could be triggered initially from an object (a Google Maps one) that is created outside the scope of an Angular2 component...