【问题标题】:How to get the data of multiple cities using openweathermap api in angularjs?如何在angularjs中使用openweathermap api获取多个城市的数据?
【发布时间】:2016-12-27 10:57:43
【问题描述】:

我是 angularjs 新手,我想在 angularjs 中获取多个城市的数据。

代码如下:

var app = angular.module('jsbin', []);    
app.controller('DemoCtrl', function($http) {

                          var vm = this;
                          var vm1 = this;                              
                          var URL = 'http://api.openweathermap.org/data/2.5/forecast/daily';

                          var request = {
                            method: 'GET',
                            url: URL,
                            params: {
                               q:'',
                              mode: 'json',
                              units: 'imperial',
                              cnt: '7',
                              appid: '4e1869d8da618fde99e84483*******'
                            }                              
                           };

                          $http(request)
                            .then(function(response) {
                              vm.data = response.data.list  ;
                              console.log(response.data);
                            }).
                            catch(function(response) {
                              vm.data = response.data;
                            });
});

【问题讨论】:

  • 那么问题出在哪里?你已经知道该怎么做了。
  • 但这里我只获取一个城市的数据,但我想要多个城市的数据。

标签: angularjs openweathermap


【解决方案1】:

我想这就是你要找的东西:

var URL = http://api.openweathermap.org/data/2.5/group?id=524901,703448,2643743&units=metric

来源:http://openweathermap.org/current#severalid

编辑:

如果端点没有为您切割,您可能必须使用 $q.all 来并行执行多个 'http://api.openweathermap.org/data/2.5/forecast/daily'

【讨论】:

  • 通过这种方式,我们无法获取多个城市的数据。
  • @Jverma 每个城市提出多个请求?
  • 谢谢@Chris,但是如何为每个城市提出多个请求?
猜你喜欢
  • 1970-01-01
  • 2013-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-24
  • 2016-12-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多