2017-03-02 35 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("&"); 

...但如上所述。這是由圖書館爲您完成的。

相關問題