簡體   English   中英

從工廠返回$ http Promise時未定義

[英]Undefined when returning $http promise in controller from factory

無論我做什么,我總是從工廠API調用中獲得$$stateundefined 我已經嘗試過Promise並只是從.then返回response.data .then但是我沒有嘗試過。

我可以將正確的響應數據輸入到控制器中,但是當我嘗試將其分配給任何對象時,我只會得到undefined$$state ,這取決於我使用的方法。

我的工廠:

factory('forecastFactory', function ($http, $q, SundialConfig) {
    var Forecast = {}; 
    var weatherKey = SundialConfig.openWeatherKey;

    Forecast.dayCnt = 1; 
    Forecast.prepareCity = function (city) {
        city === undefined ? city = 'Chicago, IL' : city = city;
        return city; 
    }

    Forecast.getForecast = function (city) {
        var preparedCity = Forecast.prepareCity(city);
        var deferred = $q.defer();

        $http.jsonp('http://api.openweathermap.org/data/2.5/forecast/daily?', {
            params: {
                appid: weatherKey,
                q: preparedCity,
                cnt: Forecast.dayCnt,   
                callback: 'JSON_CALLBACK'
            }
        })
        .then(function (res) {
            console.log("success");
            deferred.resolve(res);
        })
        .catch(function (err) {
            console.log('error');
        });

        return deferred.promise; 
    }

    return Forecast;
}); 

我的控制器:

controller('ForecastController', function ($scope, $location, forecastFactory, locationService) { 
    vm = this; 
    forecastFactory.getForecast('Chicago, IL').then(function (res) {
        console.log(res);
        vm.forecast = res; 
    });
});

我認為您不需要使用$q因為$ http返回了一個promise,

你可以做

Forecast.getForecast = function(city) {
        var preparedCity = Forecast.prepareCity(city);
        return $http.jsonp('http://api.openweathermap.org/data/2.5/forecast/daily?', {
            params: {
                appid: weatherKey,
                q: preparedCity,
                cnt: Forecast.dayCnt,   
                callback: 'JSON_CALLBACK'
            }
        })
        .then(function(res) {
      console.log("success");
      return res.data;

    })

    .catch(function(err) {
      console.log('error')
      return []; // or {} depending upon required data
    });
    }

在控制器中,執行與現在相同的操作

另一種方法是簡單地返回$ http返回的promise

Forecast.getForecast = function(city) {
        var preparedCity = Forecast.prepareCity(city);

        return $http.jsonp('http://api.openweathermap.org/data/2.5/forecast/daily?', {
            params: {
                appid: weatherKey,
                q: preparedCity,
                cnt: Forecast.dayCnt,   
                callback: 'JSON_CALLBACK'
            }
        }) 
    }

並在控制器中執行此操作

Sundial.Controllers.

controller('ForecastController', ['$scope', '$location', 'forecastFactory', 'locationService', function($scope, $location, forecastFactory, locationService) { 

    vm = this; 

    forecastFactory.getForecast('Chicago, IL').then(function(res) {
        console.log(res)
        vm.forecast = res.data; 
    }, function(err){
          // do something
     })

}]); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM