我一直在試圖解決這個問題,但沒有成功。我明白,getCurrentPosition()返回一個承諾,並在.then()函數中調用$ scope.map.center將工作。我也明白,$ scope在摘要循環中運行,並且它的屬性和對象在promise解決時更新。我對這一切都很陌生,我希望自己有道理。
這就是說,我完全喜歡瞭解發生了什麼,因爲console.log($ scope.map)按預期工作,{{map.center}}按預期顯示,但console.log(map.center)返回.then()函數之外的空對象。
在此先感謝您的幫助!
$scope.map = {
control: {},
center: {},
zoom: 16
};
$geolocation.getCurrentPosition({
timeout: 60000,
maximumAge: 250,
enableHighAccuracy: true
}).then(function(position) {
$scope.map.center.latitude = position.coords.latitude;
$scope.map.center.longitude = position.coords.longitude;
});
//Output
console.log($scope.map); //returns full object including data within $scope.map.center
console.log($scope.map.center); //returns an empty object. I am looking to convert this to an array using lodash.
{{ map.center }} works fine and returns the coordinates of my current location
**編輯(控制檯的上載的圖像)**
This is what I see in the console. First object is $scope.map Second object is $scope.map.center
感謝您的詳細解答!原諒我,如果我錯過了你的解釋,但我想知道爲什麼$ scope.map首先工作。 – nubianrover
它不是真的沒有,你只是得到你在控制器功能頂部創建的對象,或者可能部分完成了調用。 –
在做了一些更多的檢查之後,我現在認爲它與HTML5地理位置返回稱爲座標的特殊對象類型有關。這會滿足我現在的好奇心。但是,那麼我可能是錯的。 – nubianrover