-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
74 lines (43 loc) · 1.94 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//MODULE
var weatherApp = angular.module('weatherApp', ['ngRoute', 'ngResource']);
//ROUTES
weatherApp.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'pages/home.html',
controller: 'homeController'
})
.when('/forecast', {
templateUrl: 'pages/forecast.html',
controller: 'forecastController'
})
.when('/forecast/:days', {
templateUrl: 'pages/forecast.html',
controller: 'forecastController'
})
});
// SERVICES
weatherApp.service('cityService', function() {
this.city = "New York, NY";
});
// CONTROLLERS
weatherApp.controller('homeController', ['$scope', 'cityService', function($scope, cityService) {
$scope.city = cityService.city;
$scope.$watch('city', function() {
cityService.city = $scope.city;
});
}]);
weatherApp.controller('forecastController', ['$scope', '$resource', 'cityService', '$routeParams', function($scope, $resource, cityService, $routeParams) {
$scope.city = cityService.city;
$scope.days = $routeParams.days || '2';
$scope.weatherAPI =
$resource("http://api.openweathermap.org/data/2.5/forecast/daily", { callback: "JSON_CALLBACK" }, { get: { method: "JSONP" }});
//Neste ponto, foi preciso coloca o appid, pois agora é preciso, mas na epoca do tutorial não era preciso.
$scope.weatherResult = $scope.weatherAPI.get({ q: $scope.city, cnt: $scope.days, appid:'2de143494c0b295cca9337e1e96b00e0' });
$scope.convertToCelsius = function(degK) {
return Math.round(degK - 273);
}
$scope.convertToDate = function(dt) {
return new Date(dt * 1000);
};
}]);