【发布时间】:2018-07-28 22:16:17
【问题描述】:
我有一个问题,我尝试将 onEachFeature 方法用于 geoJSON 层。我尝试为每个功能分配一个点击监听器。 问题是,当我点击某个功能时,我总是会收到该错误:
未捕获的类型错误:无法读取未定义的属性“detectChanges”
我可以认为这是因为在构造函数运行之前分配了图层,但在 ngOnInit 函数中执行此操作也不起作用。如果有一个好方法会很酷:)
constructor(private changeDetector: ChangeDetectorRef){}
fitBounds: LatLngBounds;
geoLayer = geoJSON(statesData, {onEachFeature : this.onEachFeature});
onEachFeature(feature , layer) {
layer.on('click', <LeafletMouseEvent> (e) => {
this.fitBounds = [
[0.712, -74.227],
[0.774, -74.125]
];
this.changeDetector.detectChanges();
});
}
layer: Layer[] = [];
fitBounds: LatLngBounds;
onEachFeature(feature , layer : geoJSON) {
layer.on('click', <LeafletMouseEvent> (e) => {
console.log("tets"+e.target.getBounds().toBBoxString());
this.fitBounds = [
[0.712, -74.227],
[0.774, -74.125]
];
this.changeDetector.detectChanges();
});
}
constructor(private changeDetector: ChangeDetectorRef){}
ngOnInit() {
let geoLayer = geoJSON(statesData, {onEachFeature : this.onEachFeature});
this.layer.push(geoLayer);
}
【问题讨论】:
标签: leaflet angular5 ngx-leaflet