2

美好的一天,我正在使用离子角度构建天气应用程序,但JSON数据中的天气数组有问题。

我的代码

(function() {
    var app = angular.module('starter.wund', []);
    app.controller('ForecastController', function($scope, $http) {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position) {
                var key = 'xxxxxxxx'; http://www.wunderground.com/
                var service_url = "http://api.wunderground.com/api/" + key + "/forecast/geolookup/conditions/lang:AR/q/" + position.coords.latitude + "," + position.coords.longitude + ".json?callback=JSON_CALLBACK";
                $http.jsonp(service_url).success(function(data) {
                    $scope.city = data.location.city;
                    $scope.temperature = data.current_observation.temp_c;
                    $scope.description = data.current_observation.weather;
                    $scope.wind_condition = data.current_observation.wind_string;
                    $scope.weather_icon_src = data.current_observation.icon_url;
                    for(i=0; i<36; i++){
                        var day = data.forecast.txt_forecast.forecastday[i].fcttext_metric;
                        $scope.forecastfuture = [];

                        $scope.forecastfuture.push({
                            id: i,
                            day: day,
                        });
                    }
                });
            }, function() {
                alert("That's weird! We couldn't find you!");
            });
        } else {
            alert('Geolocation is not supported');
        }
    });
})();

我面临问题的代码是这样的:

for(i=0; i<36; i++){
    var day = data.forecast.txt_forecast.forecastday[i].fcttext_metric;
    $scope.forecast = [];

    $scope.forecast.push({
        id: i,
        day: day,
    });
}

错误:data.forecast.txt_forecast.forecastday[i] 在角度中未定义

4

0 回答 0