Having trouble resolving the service data in AngularJS controller.
var kattaApp = angular.module('kattaApp', []).controller('kattaController', function($scope, dataFactory) {
var promise = dataFactory.getResult().then(function(data) {
return data;
console.log(data);
});
promise.then(function(data) {
$scope.message = data;
console.log(data.geonames[0].countrycode);
});
console.log(" :'( Scope is not working " + $scope.message);
});
angular.module('kattaApp').factory('dataFactory', function($http, $q) {
return {
getResult: getResult
};
function getResult() {
var deferred = $q.defer();
$http.get('http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo').success(function(data, status) {
deferred.resolve(data);
}).error(function(data) {
deferred.reject(data);
});
return deferred.promise;
};
});
The code above attempts to assign a value to $scope.message
. The service call initially returns undefined data which later gets populated and logged in the console.
Check out my implementation on Plnkr Link