我有一条指令,可将文本输入到Google的地点自动完成输入中,如下所示:
import { Directive, AfterViewInit , Output, EventEmitter, NgZone, ElementRef } from '@angular/core'
// import { MapsAPILoader } from '@agm/core'
import { MapsAPILoader } from '@laurentgoudet/agm-core';
@Directive({
selector: '[locationAutocomplete]'
})
export class LocationAutocompleteDirective implements AfterViewInit {
@Output() locationChange: EventEmitter<any> = new EventEmitter();
constructor(
private mapsLoader: MapsAPILoader,
private ngZone: NgZone,
private searchElementRef: ElementRef
) { }
ngAfterViewInit() {
this.mapsLoader.load().then(() => {
let autocomplete = new google.maps.places.Autocomplete(this.searchElementRef.nativeElement, {
//types: ["address"]
});
autocomplete.addListener("place_changed", () => {
this.ngZone.run(() => {
// Get the place result
let place: any = autocomplete.getPlace();
// Verify result
if (place.geometry === undefined || place.geometry === null) {
return;
}
place.originalAddress = this.searchElementRef.nativeElement.value
this.locationChange.emit(place);
});
});
});
}
}
当我尝试使用Universal投放应用程序时,问题就开始了。我收到以下错误:
ReferenceError:未定义google
是否有解决方法?
最简单的方法是将其放在platformIsBrowser
下,但是在页面加载时我需要它工作。