gpt4 book ai didi

javascript - 使用 AngularJS 和 OpenWeather 创建天气应用程序

转载 作者:行者123 更新时间:2023-12-03 08:29:33 27 4
gpt4 key购买 nike

我的任务是通过 API 显示天气预报。

我有以下代码,但无法显示数据。我今天刚刚开始学习 AngularJS 并使用 API,因此我们将不胜感激!具体来说,我的代码有什么问题导致天气数据无法显示?

这是我需要使用的 API:

 http://api.openweathermap.org/data/2.5/forecast/daily?q=KansasCity&mode=json&units=imperial&cnt=7&appid=bd82977b86bf27fb59a04b61b657fb6f

angular.module('starter', ['ionic'])

.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
})

.controller('OpenWeather', function($scope, $http, $log) {
$scope.city = "Kansas City";
$scope.units = 'imperial';

$scope.change = function() {
var url = 'http://api.openweathermap.org/data/2.5/forecast/daily?q=KansasCity&mode=json&units=imperial&cnt=7&appid=bd82977b86bf27fb59a04b61b657fb6f';
$http.jsonp(url)
.success(function(data, status, headers, config) {
$scope.main = data.main;
$scope.wind = data.wind;
$scope.description = data.weather[0].description;
})
.error(function(data, status, headers, config) {
$log.error('Could not retrieve data');
});
};

$scope.change();
});

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title>Weather App</title>

<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">

<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->

<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>

<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>

<!-- your app's js -->
<script src="js/app.js"></script>
</head>
<body ng-app="starter" ng-controller="OpenWeather">
<ion-pane>
<ion-header-bar class="bar-positive">
<h1 class="title">Kansas City Weather</h1>
</ion-header-bar>
<ion-content>
<div class="list card">
<div class="item item-avatar">
<img src="img/ionic.png">
<h2>10 Day Forecast</h2>
<p>Kansas City, MO</p>
</div>
<div class="item item-avatar">
<h3>{{data.name}}</h3>
<p>Temp: {{main.temp}}</p>
<p>
</div>
</div>
</ion-content>
</ion-pane>
</body>
</html>

更新

解决了眼前的问题。感谢大家

最佳答案

您使用 $http 的方式是错误的。创建请求对象并将参数放在那里会更好、更干净。请这里是官方文档:https://docs.angularjs.org/api/ng/service/$http#usage

这里是 JSBIN:http://jsbin.com/doqeselile/edit?html,css,js,output

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

app.controller('DemoCtrl', function($http) {

var vm = this;

var URL = 'http://api.openweathermap.org/data/2.5/forecast/daily';

var request = {
method: 'GET',
url: URL,
params: {
q: 'KansasCity',
mode: 'json',
units: 'imperial',
cnt: '7',
appid: 'bd82977b86bf27fb59a04b61b657fb6f'
}
};

$http(request)
.then(function(response) {
vm.data = response.data;
}).
catch(function(response) {
vm.data = response.data;
});
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Angular JS</title>
</head>
<body ng-app="jsbin">
<div ng-controller="DemoCtrl as vm">
<h1>{{ vm.data | json }}</h1>
</div>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.js"></script>
</body>
</html>

关于javascript - 使用 AngularJS 和 OpenWeather 创建天气应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33404172/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com