还是不行。好吧,我想我可以通过将 .bind(this) 添加到我的回调中来解决范围问题,如下所示:
console.log("THIS OUTSIDE listener callback:", this)
google.maps.event.addListenerOnce(this.map, 'idle', function () {
this.loaded = true;
console.log("THIS INSIDE listener callback:", this)
}.bind(this));
控制台输出为:
"THIS OUTSIDE listener callback: MapaComponent {loaded: false, latitude: 31.7413, longitude: -60.5115, bounds: _.ae, zoom: 12…}"
"THIS INSIDE listener callback: MapaComponent {loaded: true, latitude: -31.7413, longitude: -60.5115, bounds: _.ae, zoom: 12…}"
所以...我想范围问题已经解决了。
但是,我的微调器没有隐藏,加载页面时看不到我的地图。
如果我导航到我的应用程序中的另一个页面,然后返回到我的地图页面,那么微调器会隐藏并且一切正常。
我的组件代码(总结)是:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'mapa-global',
template: `
<div class="container" [hidden]="loaded">
<div class="preloader-wrapper active" id="loader">
<div class="spinner-layer spinner-red-only">
<div class="circle-clipper left">
<div class="circle"></div>
</div><div class="gap-patch">
<div class="circle"></div>
</div><div class="circle-clipper right">
<div class="circle"></div>
</div>
</div>
</div>
</div>
<div id="map-canvas-container" [hidden]="!loaded">
<div id="map-canvas"></div>
</div>
`,
styleUrls: ['./mapa.component.css']
})
export class MapaComponent implements OnInit {
public loaded: boolean = false;
private latitude: number = -31.7413;
private longitude: number = -60.5115;
private bounds: any = new google.maps.LatLngBounds();
private zoom: number = 12;
map: any;
ngOnInit() { this.initMap(); }
initMap() {
this.map = new google.maps.Map(document.getElementById('map-canvas'), {
center: { lat: this.latitude, lng: this.longitude },
zoom: this.zoom
});
console.log("THIS OUTSIDE listener callback:", this)
google.maps.event.addListenerOnce(this.map, 'idle', function () {
this.loaded = true;
console.log("THIS INSIDE listener callback:", this)
}.bind(this));
}
}
有什么建议吗?也许 [hidden] 指令的实现方式不对?