2015-05-21 176 views
1

我正在開發Ionic的應用程序,並且我是全新的角度js,cordova和離子。AngularJS/Ionic工廠返回不同的值

問題是,我正在嘗試創建一個服務,它向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); 
    }); 

    } 

但現在我正在學習創建,因爲我需要這個使用不止一次,但在工廠的回報比在控制器不同你可以看到一個日誌的形象在這裏:Log

第一個是工廠返回 ,因爲您可以看到數據與第二部分不同,那就是直接在控制器中的響應,即http.get。

我的問題是,當我試圖從工廠讀取任何東西時,它總是拋出一個錯誤。在這裏,你必須爲工廠代碼:

var forec = ForecastService.getForecastResponse("UK", "London"); 
var day = forec.getDay(1); 

回答

2

你是正確的return$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; 
      } 
     } 

    }) 

,並在這裏控制器代碼給該工廠調用

getForecastResponse: function(country, city){ 
    return $http.get(...) 
} 

這會返回您需要做的承諾.then(或.success$http-具體的承諾,但我不推薦它),因爲當您嘗試執行.getDay時尚未收到回覆。

因此,在控制器:

ForecastService 
    .getForecastResponse("UK", "London") 
    .then(function(){ 
    var day = ForecastService.getDay(1); 
    }); 
+0

謝謝你,我開始覺得我錯了一切!這工作:) – ZetaPR

+0

小心,仍然...您使用'.success',雖然在你的例子中的作品,可能表明一些誤解。 '.success'與'.than'稍微有意義的方式不同 - 例如,'return wresp;'(在'.success'中)完全沒用,因爲這個返回沒有被任何東西捕獲。 –

+0

謝謝!如果有人想知道如何閱讀下一個承諾中的承諾,可以在這裏閱讀:http://stackoverflow.com/a/16312760/1099945 – gyosifov