0

大家好,我一直在尝试使用站点提供的 api 和 angularjs 中的 ng-repeat 功能对来自 openweathermap.org 的数据进行排序。出于某种原因,我似乎无法让它工作......我要做的是首先显示所有收集的数据,然后使用输入字段对其进行排序。

Javascript

var app = angular.module('weatherApp', []);

app.controller('weatherController', function($scope, $http) {

$http.jsonp('http://api.openweathermap.org/data/2.5/weather', { params : {
	q : $scope.city,
	units : $scope.units,
	callback: 'JSON_CALLBACK',
	APPID: $scope.id
}}).success(function(data){
	$scope.data = data;
});
	
$scope.units = 'metric';
$scope.id = 'e08f9689f06c1b6eddb44396c749eb54';  

$scope.reset = function(){
return $scope.city = "";
};
});
HTML

<div ng-app="weatherApp" ng-controller="weatherController">
<input ng-model="city.name" placeholder="City" />
<button ng-click="reset()">Reset</button>
<ul>
    <li ng-repeat=" x in data | filter : city">
        {{x.name}}
    </li>
</ul>

4

1 回答 1

0

您可以尝试在调用之前设置$scope.idand 。在我看来,这些值在设置之前已在参数中使用。尝试将其更改为:$scope.units$http.jsonp

var app = angular.module('weatherApp', []);

app.controller('weatherController', function($scope, $http) {

    $scope.units = 'metric';
    $scope.id = 'e08f9689f06c1b6eddb44396c749eb54';  

    $http.jsonp('http://api.openweathermap.org/data/2.5/weather', { params : {
        q : $scope.city,
        units : $scope.units,
        callback: 'JSON_CALLBACK',
        APPID: $scope.id
    }}).success(function(data){
        $scope.data = data;
    });

    $scope.reset = function(){
        $scope.city = '';
    };
});
于 2015-10-31T19:27:05.780 回答