0
我創建了一個涉及從API獲取位置和獲取數據並返回結果的工廠。當我打電話從控制器工廠,我得到Uncaught TypeError: Cannot read property 'then' of undefined
未捕獲的類型錯誤:無法讀取未定義的屬性'then'
廠:
app.factory("getStations", function($http) {
var stations = function() {
console.log("stations function");
console.log("GetStations Factory Loaded");
navigator.geolocation.getCurrentPosition(function (position) {
var latLng = { 'lat': position.coords.latitude, 'lng': position.coords.longitude };
console.log("This is in location service: " + latLng["lat"] + "," + latLng["lng"]);
var url = "http://dev.url.com/location/" + latLng["lat"] + "/" + latLng["lng"] + "/5";
console.log(url);
return $http({ method: "GET", url: url }).then(function (result) {
return result.data.stations;
});
});
};
console.log("return");
console.log({ stations: stations });
return { stations: stations };
});
控制器:
app.controller("PullToRefresh", function($scope, getStations) {
//
console.log("Pull To Refresh Loaded");
$scope.items = [];
$scope.doRefresh = function() {
var promise = getStations.stations();
promise.then(function(result) {
$scope.items = result;
$scope.$broadcast("scroll.refreshComplete");
});
};
});
我失去的東西,如果我把$http
出navigator
它的工作原理第二次。找到位置需要幾秒鐘,並且在第一次通話時,沒有定義var
,我收到一個URL錯誤。
你'stations'功能在'getStations'工廠不返回任何 – Phil
我想那是的情況下,我想我有點迷路,如何讓工廠從http呼叫中返回數據,當位置成功時。 –