2017-03-02 73 views
1

我遵循一个角度教程,并且由于自从教程制作后我必须在服务中使用的链接发生更改,我很困惑如何使用第二个params中的参数,这是api.openweathermap.org现在需要的appid。

function weatherFactory($http) { 
    return { 
     getWeather: function(city, country) { 
      var query = city + ', ' + country; 
      var key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; 
      return $http.get('http://api.openweathermap.org/data/2.5/weather?', { 
       params: { 
        q: query, 
        appid: key 
       } 
      }).then(function(response) { 
       // //then() returns a promise which 
       // is resolved with return value of success callback 
       return response.data.weather[0].description; 
      }); 
     } 
    }; 
} 

链接应该是这样的:

http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b1b15e88fa797225412429c1c50c122a1 

所以我把喜欢在获得第二个PARAMS的关键,但我不知道如何在应用标识的前面加上这个&,以就像从他们的链接例子。 有人可以告诉我怎么做吗?

+0

你的问题到底是什么? ,如何创建url?,如何添加params? –

回答

2

,但我不知道如何的appid的前面加上这个&,要像从他们

链接例子

&是查询字符串的分隔符。在将传递给params的对象转换为查询字符串时应该添加它。

您没有将对象转换为查询字符串。角是。所以什么都不做。 Angular会为你做到这一点。


如果您手动转换它,你会做这样的事情:

var pairs = []; 
Object.keys(obj.params).forEach(function (key) { 
    pairs.push(encodeURIComponent(key) + "=" + encodeURIComponent(params[key])); 
}); 
var query_string = pairs.join("&"); 

...但如上所述。这是由图书馆为您完成的。

相关问题