我开发了一个AngularJS天气应用程序,我唯一要解决的问题与我的输入元素有关。提交值后,输入值将替换为“[object Object]”。我已经阅读了几个问题,到目前为止还没有找到帮助我解决这个问题的任何问题。所以我转向你们,希望你能帮助我。
表格和输入如下:
<form name="add-city" ng-submit="getCity(city)">
<input id="user-city" type="text" name="user-city" placeholder="Search for a city" ng-model="city" ></input>
<button type="button" ng-click="getCity(city)">Add</button>
</form>
具有getCity(city)功能的控制器如下:
app.controller('MainController', ['$scope','forecasts','$http', function($scope, forecasts, $http){
forecasts.success(function(data){
$scope.forecasts = data.list;
});
$scope.removeCity = function (index) {
$scope.forecasts.splice(index, 1);
urlCityList.splice(index, 1);
localStorage.setItem('myCities', JSON.stringify(urlCityList));
};
$scope.getCity = function(city) {
console.log('getCity() invoked with: '+city);
var thisCity=$scope.city;
// CONSTRUCT URL
var url1 = 'http://api.openweathermap.org/data/2.5/weather?q=';
var url2 = '&units=metric&APPID=f96088515ba4137e85db097513d9d8ab';
var getCityUrl = url1+$scope.city+url2;
$http.get(getCityUrl)
.success(function(data){
$scope.city = data;
var newCity = {
"city": $scope.city.name,
"country":$scope.city.sys.country,
"id":$scope.city.id
}
var getCorrectCity = newCity.city.toUpperCase() == city.toUpperCase();
var avoidDuplicate = true;
var noDuplicate = true;
for (i=0; i < urlCityList.length; i++) {
if (urlCityList[i].city.toUpperCase() == city.toUpperCase()) {
noDuplicate = false;
}
}
if (getCorrectCity == true && noDuplicate == true) {
urlCityList.push(newCity);
localStorage.setItem('myCities', JSON.stringify(urlCityList))
$scope.forecasts.push($scope.city);
}
})
.error(function(err){
return err;
});
console.log($('#addCity').val());
$('#addCity').val('');
};
$("#user-city").keyup(function (e) {
if (e.keyCode == 13) {
// Do something
}
});
}]);
应用程序已启用,问题可以在这里看到:jhalland.dk/weather
提前致谢