说我正在发出$ http请求
$http.get('http://api.worldweatheronline.com/free/v2/weather.ashx?q=Manchester&format=json&num_of_days=5&key=1e9bfc54489901fe117e6f2877018')
.success(function(response) {
$scope.worldweather = response;
});
但我想用范围值替换网址中的城市名称,我想知道id可以做到这一点,但可惜没有。有什么建议?
$scope.city = 'Manchester';
$http.get('http://api.worldweatheronline.com/free/v2/weather.ashx?q=' + city + '&format=json&num_of_days=5&key=1e9bfc54489901fe117e6f2877018')
.success(function(response) {
$scope.worldweather = response;
});
city
是当前上下文中的变量。$scope.city
是$scope
对象的属性。它们不是同一件事。尝试:
$http.get('http://api.worldweatheronline.com/free/v2/weather.ashx?q=' + $scope.city + '&format=json&num_of_days=5&key=1e9bfc54489901fe117e6f2877018')
但是通常,使用params来$http
查询字符串比手动构建它更容易
$http.get('http://api.worldweatheronline.com/free/v2/weather.ashx',{
params: {
q:$scope.city,
format:json,
num_of_days:5,
key: 1e9bfc54489901fe117e6f2877018
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句