2016-05-23 29 views
1

我有這樣的:沒有使用ngClass與谷歌地圖縮放事件綁定映射

<i [ngClass]="{'active': isGeoLocationButtonEnabled }" class="toggle fa fa-crosshairs" > 

而且處理程序是:

private onStartZoomEvent(event) { 
    if (event.zoom != 12) { 
     console.log("Current zoom level is: ", event.zoom); 
     this._eventAggregator.trigger(new DisableGeolocationEvent()); 
     this.disableGeolocationButton(); 
    } 
} 

被觸發看起來像這樣的事件:

private mapZoomChangedHandler() { 
    this._mapsWrapper.getMap().then((map: google.maps.Map) => { 
     let zoom = map.getZoom(); 
     this._eventAggregator.trigger(new MapStartZoomEvent(zoom)); 
    }); 
} 

我正在這樣訂閱:

this._eventAggregator.subscribe(MapStartZoomEvent, this.onStartZoomEvent.bind(this)); 

問題是,我第一次更改地圖縮放時,該按鈕未被禁用。第二次點擊它,它被禁用。當我調試時,一切似乎都沒問題,我的布爾isGeoLocationButtonEnabled設置爲false,但活動類保持不變(這只是背景色突出顯示)。

+0

運行,更新Angulars區內的模型的代碼如何(在哪裏)你初始化'isGeoLocationButtonEnabled'財產? –

+0

public isGeoLocationButtonEnabled:boolean = false;也在構造函數中this.isGeoLocationButtonEnabled = false;既沒有工作 –

回答

2

您不顯示isGeoLocationButtonEnabled的更新位置。我很確定Google地圖代碼在Angulars區域之外運行。

使用zone.run(...)所以變化檢測是繼調用

constructor(private zone:NgZone) {} 

methodWhereEnabledIsUpdated() { 

    ... 
    this.zone.run(() => { 
    isGeoLocationButtonEnabled = someValue; 
    }); 
    ... 
} 
+0

謝謝你,情況就是如此 –