I'm learning Angular by making a weather app. So, In index.html I have a dropdown list.
<select ng-model="city" ng-options="city.cityName for city in cities" ng-change="getWheather()">
<option value=""></option>
</select>
Depending on what city user selects from that list, he gets a weather forecast. It was all working perfectly while the function getWheather() was in the controller. Once I've put it in a factory, it is not longer working. I assume its because it is asynchronous, but I have no luck in fixing it.
This is the factory (in URL, numbers 45 and 15 should be city.latit and city.longit, but I can't get it to work. If I pass the value "city" to function getWheather(), it is undefined):
app.factory('wheatherService', function($http) {
var getWheather = function(){
var url ="https://api.forecast.io/forecast/api_key/"+45.815+","+15.966+"?callback=JSON_CALLBACK";
return $http.jsonp(url);
};
return {
getWheather: getWheather
};
});
This is the controller:
app.controller('inputCtrl', function($scope, wheatherService){
$scope.cities = [{ "cityName" : 'Paris', "latit" : 45.815399, "longit" : 15.966568},
{ "cityName" : 'London', "latit" : 45.328979, "longit" : 14.457664},
{ "cityName" : 'NewYork', "latit" : 43.510742, "longit" : 16.437489 }];
wheatherService.getWheather().success(function(data){
$scope.wheatherData = data;
console.log($scope.wheatherData);
});
});
So the main question is - is there and what is the way to get selected value from dropdown list to getWheather() function?