我正在开发Ionic的应用程序,而且我在所有这些角度js,cordova和离子上都是全新的。
问题是我正在尝试创建一个服务,要求API提供响应。我在控制器:
中使其完美运行$scope.search = function(icao) {
console.log(icao);
$scope.forecastfuture = [];
$http.get("http://api.wunderground.com/............../UK/"+icao+".json")
.success(function(data, status, headers, config){
console.log(data);
console.log(typeof data);
for(i=0; i<36; i++){
var day = data.hourly_forecast[i].FCTTIME.pretty;
var maxtemp = data.hourly_forecast[i].temp.metric;
var mintemp = data.hourly_forecast[i].dewpoint.metric;
var feelslike = data.hourly_forecast[i].feelslike.metric;
var humidity = data.hourly_forecast[i].humidity;
var windspd = data.hourly_forecast[i].wspd.metric;
var winddir = data.hourly_forecast[i].wdir.dir;
var sky = data.hourly_forecast[i].condition;
var precip = data.hourly_forecast[i].qpf.metric;
var snow = data.hourly_forecast[i].snow.metric;
var icon = data.hourly_forecast[i].icon_url;
console.log(day);
console.log(maxtemp);
$scope.forecastfuture.push({
id: i,
day: day,
maxtemp: maxtemp,
mintemp: mintemp,
feelslike: feelslike,
humidity: humidity,
windspd: windspd,
winddir: winddir,
sky: sky,
precip: precip,
snow: snow,
icon: icon});
}
})
.error(function(data, status, headers, config){
console.log(data);
});
}
但是现在我正在学习创建一个工厂,因为我需要使用不止一次,但工厂的回报不同于控制器您可以在此处看到日志图片:
第一个是工厂退货,因为您可以看到数据与第二部分不同,即控制器中的响应,即http。得到。
我的问题是,当我尝试从工厂读取任何内容时,它会一直抛出错误。您可以在此处获得工厂:
的代码.factory('ForecastService', function(CurrentPosition, $http){
var wresp;
return{
getForecastResponse: function(country, city){
return $http.get("http://api.wunderground.com/api/.........../q/"+country+"/"+city+".json")
.success(function(data, status, headers, config){
console.log(data);
wresp = data;
return wresp;
});
},
getDay: function(i){
var day = wresp.hourly_forecast[i].FCTTIME.pretty;
return day;
},
getMaxTemp: function(i){
var maxtemp = wresp.hourly_forecast[i].temp.metric;
return maxtemp;
},
getMinTemp: function(i){
var mintemp = wresp.hourly_forecast[i].dewpoint.metric;
return mintempo;
},
getFeelsLike: function(i){
var feels = wresp.hourly_forecast[i].feelslike.metric;
return feels;
},
getHumidity: function(i){
var humidity = wresp.hourly_forecast[i].humidity;
return humidity;
},
getWindSpd: function(i){
var windspd = wresp.hourly_forecast[i].wspd.metric;
return windspd;
},
getWinDir: function(i){
var windir = wresp.hourly_forecast[i].wdir.dir;
return windir;
},
getSky: function(i){
var sky = wresp.hourly_forecast[i].condition;
return sky;
},
getPrecip: function(i){
var precip = wresp.hourly_forecast[i].qpf.metric;
return precip;
},
getSnow: function(i){
var snow = wresp.hourly_forecast[i].snow.metric;
return snow;
},
getIcon: function(i){
var icon = wresp.hourly_forecast[i].icon_url;
return icon;
}
}
})
以及控制器中的代码来调用此工厂:
var forec = ForecastService.getForecastResponse("UK", "London");
var day = forec.getDay(1);
答案 0 :(得分:2)
return
调用$http
调用的结果是正确的:
getForecastResponse: function(country, city){
return $http.get(...)
}
这会返回您需要执行的承诺.then
(或.success
$http
- 特定承诺,但我不建议这样做,因为响应是在您尝试.getDay
时尚未收到。
所以,在控制器中:
ForecastService
.getForecastResponse("UK", "London")
.then(function(){
var day = ForecastService.getDay(1);
});