Greetings! Here is the code snippet for my service:
angular.module('lucho1App').factory('ApiExample', ['$http', '$q', function($http, $q) {
return {
promiseToHaveData: function() {
return $http.get('http://data.colorado.gov/resource/4ykn-tg5h.json').then(function(response) {
var result = response.data;
console.log(result);
return result;
}, function(response) {
console.log('An error has occurred');
});
}
};
}]);
Additionally, here is the controller associated with the service:
angular.module('lucho1App')
.controller('MainCtrl',['$scope', 'ApiExample',function MainCtrl($scope,ApiExample) {
$scope.apiExampl=ApiExample.promiseToHaveData();
console.log($scope.apiExampl);
}]);
In the above setup, the console.log in the service displays output indicating a successful request. However, in the controller, $scope.apiExampl
returns as undefined. What could be causing this discrepancy?