【发布时间】:2022-01-04 08:02:18
【问题描述】:
我有模式,我正在使用谷歌地图服务搜索位置。我收到TypeError: Cannot read properties of undefined (reading 'nativeElement')的错误
HTML
<input
type="text"
[ngClass]="{ error_border: submitted && f.location.errors }"
class="form-control"
formControlName="location"
[readonly]="viewMode"
(keydown.enter)="$event.preventDefault()"
placeholder="Search Location"
autocorrect="off"
autocapitalize="off"
spellcheck="off"
#search
/>
TS
export class ListComponent implements OnInit, AfterViewInit {
dbLocation:any;
latitude!: number;
longitude!: number;
zoom!: number;
address!: string;
private geoCoder:any;
searchFlag:boolean = false;
@ViewChild('search' , { static: true }) public searchElementRef!: ElementRef ;
ngOnInit(): void {
}
ngAfterViewInit() {
//----------autocomplete code block------------
//load Places Autocomplete
this.mapsAPILoader.load().then(() => {
// this.setCurrentLocation();
this.geoCoder = new google.maps.Geocoder;
let autocomplete = new google.maps.places.Autocomplete(this.searchElementRef.nativeElement);
autocomplete.addListener("place_changed", () => {
this.ngZone.run(() => {
//get the place result
let place: google.maps.places.PlaceResult = autocomplete.getPlace();
//verify result
if (place.geometry === undefined || place.geometry === null) {
return;
}
//set latitude, longitude and zoom
this.latitude = place.geometry.location.lat();
this.longitude = place.geometry.location.lng();
this.getAddress(this.latitude, this.longitude);
this.zoom = 15;
this.searchFlag = true;
});
});
});
//----------autocomplete code block------------
}
}
我已尝试将 { static: true } 添加到 viewChild。还将自动完成代码块从 ngOnInit 移动到 ngAfterViewInit。还为自动完成代码块添加超时。但仍然报错。
如果输入字段不在ngOnInit 中的模态和自动完成代码块上,它不会给出错误。但是我有模态表单,其中有位置输入字段。出现错误。
我无法弄清楚哪种代码组合会起作用。我该如何解决这个错误?
请帮助和指导。
编辑 模态 HTML
<ng-template #formModal let-modal>
<div class="modal-header">
<h5 class="modal-title" id="modal-basic-title">
Form
</h5>
<button
type="button"
class="close"
aria-label="Close"
(click)="modal.dismiss('Cross click')"
>
<span aria-hidden="true">
<i class="fas fa-times"></i>
</span>
</button>
</div>
<div class="modal-body">
<form [formGroup]="form">
<div class="row edit_profile">
<div class="col-sm-12">
<div class="form-group">
<label>Name<b style="color: red" *ngIf="!viewMode">*</b></label>
<input
type="text"
[ngClass]="{ error_border: submitted && f.headline.errors }"
formControlName="headline"
class="form-control"
[readonly]="viewMode"
/>
<div *ngIf="submitted && f.headline.errors" class="text-danger">
<div *ngIf="f.headline.errors.required">
name is required
</div>
</div>
</div>
</div>
<div class="col-sm-7">
<div class="form-group">
<label for="location">Location <b style="color: red" *ngIf="!viewMode">*</b></label>
<div class="custome_input icons date_icon">
<i class="fas fa-map-marker-alt"></i>
<input
type="text"
[ngClass]="{ error_border: submitted && f.location.errors }"
class="form-control"
formControlName="location"
[readonly]="viewMode"
(keydown.enter)="$event.preventDefault()"
placeholder="Search Location"
autocorrect="off"
autocapitalize="off"
spellcheck="off"
#search
/>
</div>
<div *ngIf="submitted && f.location.errors" class="text-danger">
<div *ngIf="f.location.errors.required">Location is required</div>
</div>
</div>
</div>
<div class="form-group btn-group mb-0" *ngIf="!viewMode">
<button
[disabled]="loading"
class="btn"
(click)="onSubmitForm()"
>
<span
*ngIf="loading"
class="spinner-border spinner-border-sm mr-1"
></span>
Save
</button>
</div>
</form>
</div>
</ng-template>
开启模态的TS
open(content) {
this.listModalRef = this.modalService.open(content,{ size: 'lg', backdrop: 'static' });
this.listModalRef.result.then((result) => {
this.closeResult = `Closed with: ${result}`;
}, (reason) => {
this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
});
setTimeout(() => {
//load Places Autocomplete
this.mapsAPILoader.load().then(() => {
// this.setCurrentLocation();
this.geoCoder = new google.maps.Geocoder;
let autocomplete = new google.maps.places.Autocomplete(this.searchElementRef.nativeElement);
autocomplete.addListener("place_changed", () => {
this.ngZone.run(() => {
//get the place result
let place: google.maps.places.PlaceResult = autocomplete.getPlace();
//verify result
if (place.geometry === undefined || place.geometry === null) {
return;
}
//set latitude, longitude and zoom
this.latitude = place.geometry.location.lat();
this.longitude = place.geometry.location.lng();
this.getAddress(this.latitude, this.longitude);
this.zoom = 15;
this.searchFlag = true;
});
});
});
}, 0)
}
【问题讨论】:
-
在使用
@ViewChild时,只需添加一个小注释{ static: false}是默认标志。 -
@Chaka15 更新问题。
-
名称“搜索”可能已被使用,您是否尝试为该元素使用其他名称?
-
@TheFabio 是的,也尝试过更改名称。不工作
-
你试过 searchElementRef!: ElementRef without !, as searchElementRef: ElementRef (and without static:true)?
标签: angular typescript google-maps viewchild googleplacesautocomplete