Unable to fetch value from array of object


#1

i want to get correspoding value of object from cities on selecting item from list
(like lat, lng, name). but i am not getting it. i always get default value from DATASTORE.

here is my service.js folder

‘use strict’;

var forecastioWeather = [’$q’, ‘$resource’, ‘$http’, ‘FORECASTIO_KEY’,
function($q, $resource, $http, FORECASTIO_KEY) {
var url = ‘https://api.darksky.net/forecast/’ + FORECASTIO_KEY + ‘/’;

var weatherResource = $resource(url, {
callback: ‘JSON_CALLBACK’
}, {
get: {
method: ‘JSONP’
}
});

return {
//getAtLocation: function(lat, lng) {
getCurrentWeather: function(lat, lng) {
return $http.jsonp(url + lat + ‘,’ + lng + ‘?callback=JSON_CALLBACK’);
}
}
}];

angular.module(‘starter.services’, [‘ngResource’])
.factory(‘Cities’, function() {
var cities = [
{ id: 0, name: ‘Miami’, lat:25.7877 , lgn: 80.2241 },
{ id: 1, name: ‘New York City’ ,lat: 40.7127 , lgn: 74.0059 },
{ id: 2, name: ‘London’ ,lat:51.5072 , lgn: 1.1275 },
{ id: 3, name: ‘Los Angeles’ ,lat: 34.0500 , lgn: 118.2500 },
{ id: 4, name: ‘Dallas’ ,lat: 32.7758 , lgn:96.7967 },
{ id: 5, name: ‘Frankfurt’ ,lat:50.1117 , lgn: 8.6858 },
{ id: 6, name: ‘New Delhi’ ,lat:28.6100 , lgn: 77.2300 }
];

return {
all: function() {
return cities;
},
get: function(cityId) {
// Simple index lookup
return cities[id];
}
}
}).
factory(‘DataStore’, function() {
//create datastore with default values
var DataStore = {
city: ‘Miami’,
latitude: 25.7877,
longitude: 80.2241 };

DataStore.setCity = function (value) {
   DataStore.city = value;
};

DataStore.setLatitude = function (value) {
   DataStore.longitude = value;
};

DataStore.setLongitude = function (value) {
   DataStore.longitude = value;
};

return DataStore;

})
.factory(‘Weather’, forecastioWeather);

// here is controller.js folder

angular.module(‘starter.controllers’, [‘ionic’])
.constant(‘FORECASTIO_KEY’, ‘b8e81236c5d6d86a778ace3641717892’)
.controller(‘HomeCtrl’, function($scope,$state,Weather,DataStore) {
//read default settings into scope
console.log(‘inside home’);
$scope.city = DataStore.city;
var latitude = DataStore.latitude;
var longitude = DataStore.longitude;

//call getCurrentWeather method in factory ‘Weather’
Weather.getCurrentWeather(latitude,longitude).then(function(resp) {
  $scope.current = resp.data;
  console.log('GOT CURRENT', $scope.current);
  //debugger;
}, function(error) {
  alert('Unable to get current conditions');
  console.error(error);
});

})
.controller(‘LocationsCtrl’, function($scope,$state, Cities,DataStore) {
$scope.cities = Cities.all();

$scope.changeCity = function(cityId) {
//get lat and longitude for seleted location
var lat = $scope.cities[cityId].lat; //latitude
var lgn = $scope.cities[cityId].lgn; //longitude
var city = $scope.cities[cityId].name; //city name

DataStore.setCity(city);
DataStore.setLatitude(lat);
DataStore.setLongitude(lgn);

$state.go('tab.home');

}
})
.controller(‘SettingsCtrl’, function($scope) {
//manages app settings
});