1

我有一个看起来像这样的数组:

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

这是我要显示 id 的 div 的标记locations

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

但它似乎不起作用!

4

1 回答 1

1

您可以尝试显式调用该类的detectChanges方法ChangeDetectorRef

constructor(private cdr:ChangeDetectorRef) {
}

(...)

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

首先检查该方法是否被正确调用location并且selectedLocation在 TypeScript 类中被更新。

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

constructor(private ngZone:NgZone) {
}

(...)

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

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

于 2016-05-27T12:18:11.307 回答