我一直在通过事件对象访问变量。 有没有什么我可以在这段代码中重构,因为我需要在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