我正在使用谷歌地圖并嘗試在用戶單擊圖釘時顯示 div。
<div *ngIf="this.viewing_place">
{{ this.viewing_place?.title || 'NO_PLACE' }}
</div>
function redrawMap(){
let self = this;
this.places.forEach(function(place){
let marker = self.map.addMarkerSync({ position :new LatLng(place.pin_lat, place.pin_long) });
marker.on(GoogleMapsEvent.MARKER_CLICK).subscribe(() => {
self.openPlaceInfo(place);
});
});
}
單擊事件運行良好,并且位置物件正在傳遞正常。
openPlaceInfo(place){
console.log(place); //===========> shows correct object
this.viewing_place = place; //======> set the page variable
}
問題是沒有顯示 div,就像沒有設定變數一樣this.viewing_place。
所以我創建了一個手動方法只是為了測驗,當我使用這個功能時,它可以正常作業。
manuallyTest(){
this.viewing_place = {title:'test place'};
}
難道我做錯了什么?這只發生在 iOS 和 Android 中。它在瀏覽器中完美運行。
uj5u.com熱心網友回復:
在移動設備中,當變數從 observable 更新時,angular 會限制呼叫變化檢測周期。當我與 Ionic 合作時,我經歷了這一點。嘗試在內部運行您NgZone的函式以強制更改檢測周期重新呈現 UI。
constructor() {
...
private zone: NgZone
}
marker.on(GoogleMapsEvent.MARKER_CLICK).subscribe(() => {
this.zone.run(() => {
self.openPlaceInfo(place);
});
});
OR
openPlaceInfo(place){
console.log(place); //===========> shows correct object
this.zone.run(() => {
this.viewing_place = place; //======> set the page variable
});
}
希望這對你有幫助!
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/496519.html
標籤:javascript 有角度的 离子框架
