-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
78 lines (59 loc) · 2.26 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
75
76
77
var weatherapp = angular.module("weatherApp", ["ngRoute", "ngResource"]);
weatherapp.service("cityService", function() {
this.city = 'London';
this.days = '';
})
weatherapp.controller("homeController", ["$scope", "$routeParams", "$location", "cityService", function($scope, $routeParams, $location, cityService) {
$scope.days = cityService.days;
$scope.city = cityService.city;
$scope.$watch('city', function(){
cityService.city = $scope.city;
})
$scope.$watch('days', function(){
cityService.days = $scope.days;
})
$scope.submit = function() {
$location.path("/").url('/forecast/' + $scope.days);
}
}])
weatherapp.controller("forecastController", ["$scope", "$routeParams", "$resource", "$location", "cityService", function($scope, $routeParams, $resource, $location, cityService) {
$scope.days = '';
// local scope variable = city variable from the cityService
$scope.city = cityService.city;
// route parameter day is equal to local scope variable for day - default parameter is set to 3 days.
$scope.days = $routeParams.days || '3';
// calling data from external API and declaring callback so we get a reply.
var weatherdata = $resource('https://api.openweathermap.org/data/2.5/forecast/daily?appid=542ffd081e67f4512b705f89d2a611b2', {callback: "JSON_CALLBACK" }, {get: { method: "JSONP" }});
// telling API what data is required and assigning to local scope variable.
weatherdata.get({q: $scope.city, cnt: $scope.days}, function(result) {
$scope.weatherresult = result;
// assign resulting data from API above to weatherResult variable.
var weatherResult = $scope.weatherresult;
// converting data from API to user readable data
$scope.convertFarheinheit = function(kelvintemp) {
return Math.round(1.8 * (kelvintemp - 273) + 32);
}
// converting date to readable format
$scope.convertDate = function(dt) {
return new Date(dt * 1000);
}
})
}])
weatherapp.config(function($routeProvider) {
$routeProvider
.when("/", {
templateUrl: 'pages/main.html',
controller: 'homeController'
})
.when("/forecast", {
templateUrl: 'pages/forecast.html',
controller: 'forecastController'
})
.when("/forecast/:days", {
templateUrl: 'pages/forecast.html',
controller: 'forecastController'
})
.otherwise({
redirectTo: "/"
});
});