【发布时间】:2017-12-04 00:36:46
【问题描述】:
在我的程序中,我正在开发一个天气应用程序,其中我有一个邮政编码立即被用户的 IP 从另一个 API 获取。 OpenWeatherMap API 从这个直接的邮政编码显示该邮政编码的天气。但我添加了一个按钮,我想在其中加入一项功能,您可以更改邮政编码并查看不同地区的天气。
在我看来,每当我添加新输入时,HTML 上名为 {{zip}} 的输入就会发生变化,但它似乎并没有更新 API。
如何更新此 API 调用的邮政编码部分?谢谢!
app.js
var classApp = angular.module('weatherApp', []);
classApp.controller('weatherCtrl', function($scope, $http) {
var vm = $scope;
$scope.count = 0;
$http.get("http://ip-api.com/json").success(function(data) {
vm.zip = data.zip;
vm.lat = data.lat;
vm.lon = data.lon;
$scope.getForecastByLocation = function(myName) {
vm.zip = myName;
console.log("this");
var apiKey = "";
var url = "http://api.openweathermap.org/data/2.5/weather?zip=" + vm.zip + ",us" + "&appid=" + apiKey;
};//getForecastByLocation
var apiKey = "";
var openWeatherURL = "http://api.openweathermap.org/data/2.5/weather?zip=" + vm.zip + ",us" + "&appid=" + apiKey;
$scope.getForecastByLocation();
// Set $scope.location and execute search on API
vm.setLocation = function(loc) {
$scope.location = loc;
$scope.getForecastByLocation();
};
$http.get(openWeatherURL).success(function(data) {
vm.description = data.weather[0].description;
vm.speed = (2.237 * data.wind.speed).toFixed(1) + " mph";
vm.name = data.name;
vm.humidity = data.main.humidity + " %";
vm.temp = data.main.temp;
vm.fTemp = (vm.temp * (9 / 5) - 459.67).toFixed(1) + " °F";
vm.cTemp = (vm.temp - 273).toFixed(1) + " °C";
vm.icon = "https://openweathermap.org/img/w/" + data.weather[0].icon + ".png";
// $scope.getForecastByLocation = function() {
// console.log("hellooooooooo");
// // alert("This is an example of ng-click");
// localStorage.setItem('zipcode', $scope.serverip);
// console.log(localStorage.serverip);
// };
//Getting the weather icon
if (data.weather[0].id >= 200 && data.weather[0].id < 300) {
$scope.weatherClass = "wi wi-thunderstorm";
}
if (data.weather[0].id >= 300 && data.weather[0].id < 400) {
$scope.weatherClass = "wi wi-sprinkle";
}
if (data.weather[0].id >= 500 && data.weather[0].id < 600) {
if (data.weather[0].id == 500 || data.weather[0].id >= 520) {
$scope.weatherClass = "wi wi-rain";
}
$scope.weatherClass = "wi wi-showers";
}
if (data.weather[0].id >= 600 && data.weather[0].id < 700) {
$scope.weatherClass = "wi wi-snow";
}
if (data.weather[0].id >= 700 && data.weather[0].id < 800) {
$scope.weatherClass = "wi wi-fog";
}
if (data.weather[0].id == 800) {
$scope.weatherClass = "wi wi-day-sunny";
}
if (data.weather[0].id == 801) {
$scope.weatherClass = "wi wi-day-sunny-overcast";
}
if (data.weather[0].id == 802) {
$scope.weatherClass = "wi wi-day-cloudy";
}
if (data.weather[0].id == 803 || data.weather[0].id == 804) {
$scope.weatherClass = "wi wi-cloudy";
}
if (data.weather[0].id == 900) {
$scope.weatherClass = "wi wi-tornado";
}
if (data.weather[0].id == 901 || data.weather[0].id == 960 || data.weather[0].id == 961) {
$scope.weatherClass = "wi wi-thunderstorm";
}
if (data.weather[0].id == 902 || data.weather[0].id == 962) {
$scope.weatherClass = "wi wi-hurricane";
}
if (data.weather[0].id == 903) {
$scope.weatherClass = "wi wi-snowflake-cold";
}
if (data.weather[0].id == 904) {
$scope.weatherClass = "wi wi-hot";
}
if (data.weather[0].id == 905) {
$scope.weatherClass = "wi wi-strong-wind";
}
if (data.weather[0].id == 906) {
$scope.weatherClass = "wi wi-hail";
}
if (data.weather[0].id == 951) {
$scope.weatherClass = "wi wi-day-sunny";
}
if (data.weather[0].id >= 952 && data.weather[0].id <= 956) {
$scope.weatherClass = "wi wi-windy";
}
if (data.weather[0].id >= 957 && data.weather[0].id <= 959) {
$scope.weatherClass = "wi wi-strong-wind";
}
// Calculate current hour using offset from UTC.
var a = new Date(data.dt * 1000);
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var year = a.getFullYear();
var month = months[a.getMonth()];
var date = a.getDate();
// Hours part from the timestamp
var hours = a.getHours();
// Minutes part from the timestamp
var minutes = "0" + a.getMinutes();
// Seconds part from the timestamp
var seconds = "0" + a.getSeconds();
vm.formattedDate = date + ' ' + month + ' ' + year;
vm.formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
vm.sunrise = new Date(data.sys.sunrise * 1000 + (scope.offsetHours * 3600000) + (scope.offsetMinutes * 60000));
vm.sunset = new Date(data.sys.sunset * 1000 + (scope.offsetHours * 3600000) + (scope.offsetMinutes * 60000));
vm.currentHour = datetime.getUTCHours();
vm.sunriseHour = sunrise.getUTCHours();
vm.sunsetHour = sunset.getUTCHours();
}); //closing OpenWeatherMap
}); //closing IP-API
}); //closing Controller
index.html
<!doctype html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<title>OpenWeather App</title>
<link href='https://fonts.googleapis.com/css?family=Montserrat:300,400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="css/weather-icons.min.css">
<link rel="stylesheet" href="css/bootstrap/css/bootstrap.css"/>
<link rel="stylesheet" href="css/app.css"/>
</head>
<body ng-app="weatherApp" ng-controller="weatherCtrl" class= "text-center info" ng-style="weatherBackground">
<div class ="header">
<div class="left">
<h3 style="text-align: left; font-size: 25px;">{{formattedDate | uppercase}}</h3>
<h3 style="text-align: left; font-size: 25px;">{{formattedTime | uppercase}}</h3><br>
</div>
</div>
<div class="right">
<div class="input">
<input ng-model="myName" type="text" placeholder="enter zipcode"><br>
<button ng-disabled="myName==null || myName==''" ng-click="getForecastByLocation(myName)" style="margin-top: 10px">Search!</button>
</div>
</div>
</div>
<br><br>
<div class="panel">
<h3 style="font-size: 25px; margin-right: 275px; margin-top: 35px">{{name | uppercase}}</h3><br>
<i ng-class="weatherClass" style="font-size:100px; margin-top: -10px;"></i>
<h3 style="text-align: center; font-size: 60px; margin-top: 15px; ">{{fTemp | uppercase}}</h3>
<p>{{description | uppercase}} <img ng-src="{{icon}}"/></p>
<p>{{location}}</p>
<a class="btn-lg btn-primary">{{speed}} </a>
<a class="btn-lg btn-primary">{{humidity}}</a>
<p>{{zip}}</p>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js"></script>
<script src="js/boo.js"></script>
</body>
</html>
编辑:添加了整个控制器代码
【问题讨论】:
-
您需要再次向天气 API 发出请求。也许,您可以编写一个接收邮政编码并返回天气的函数。然后首先为用户的邮政编码调用它,然后为用户输入的其他邮政编码调用它。
-
@NicholasKajoh 很抱歉,你能举个例子吗?一般来说,我对 angularjs/REST API 还是很陌生。 put HTTP 命令应该在 get 中吗?或者他们应该是分开的和外面的?谢谢!
-
@NicholasKajoh 我一直在谷歌搜索并试图弄清楚,但它不更新的原因是因为 JS 是异步的吗?
-
可能是异步问题。提供你的整个天气控制器代码会给我更多的上下文。我的意思是,您创建了一个接收邮政编码并返回天气的范围/函数。您可以在
ng-click上调用该函数,以便再次向 api 发送请求以获取新邮政编码的天气。天气数据应该从.then()方法返回,而不是在它还不可用的地方。 -
@NicholasKajoh 我刚刚添加了整个 app.js 代码!
标签: javascript angularjs html openweathermap