我一直在通過事件對象訪問變量。 有沒有什麼我可以在這段代碼中重構,因爲我需要在templateUrl中顯示一些註釋,然後在地圖的事件點擊中設置它們。 下面是代碼:Angular2 - 通過事件對象訪問變量
import { Component, OnInit, AfterViewInit } from '@angular/core';
declare var ol: any;
@Component({
selector: 'my-map-app',
templateUrl: 'app.component.html'
})
export class AppComponent implements OnInit, AfterViewInit {
static draw: any;
annotations: any[];
constructor() {
this.annotations = [];
}
ngOnInit(): void {
// ADD MAP
AppComponent.map = new ol.Map({
target: 'map',
layers: layers,
view: new ol.View({
center: ol.proj.fromLonLat([10, 10]),
zoom: 2
})
});
// Action when clicking on a Point (no edition mode)
AppComponent.map.on('singleclick', function(evt) {
// here I want to access and set the array of annotations
//this.annotations or AppComponent.annotations is not available
});
}
編輯:上app.component.html:
<select >
<option *ngFor="let annotation of annotations">{{annotation.name}}</option>
</select>
我更新了我的問題 - 因爲我可以記錄正確設置在我的事件中的_self對象 - 但是如果我在那裏更新它(並且我需要它):視圖仍然不是。有沒有辦法做到這一點? – F3L1X79
好吧,最後奇怪的是,視圖有時*更新,有時不是 - 當不是時,我需要更改我的選擇值以查看已更新的列表。如果有任何線索,我將不勝感激。 – F3L1X79