Angular2 - 在 SebmGoogleMapMarker 事件 "markerClick" 触发后,视图未更新

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

我有一个如下所示的数组:

    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'}
];

如果我点击一个标记,它会打开一个包含所选标记信息的部分。

这里是 googlemaps 的html:

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

这里是updateDiv()函数:

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

这是 div 的标记,我想在其中显示 locations

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

但是好像不行!

您可以尝试显式调用 ChangeDetectorRef class 的 detectChanges 方法:

constructor(private cdr:ChangeDetectorRef) {
}

(...)

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

首先检查该方法是否实际被正确调用 location 并且 selectedLocation 在 TypeScript class.

中更新

如果这不起作用,您可以利用 NgZone:

constructor(private ngZone:NgZone) {
}

(...)

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

最初可以从在 Angular2 组件范围之外创建的对象(Google 地图对象)触发处理...