My $http
call in AngularJS is returning undefined when I try to access the data in my controller. What could be causing this issue?
Despite using .then to handle promises, the data passed to the controller seems to become undefined. Can you help me figure out what's wrong?
JavaScript
var app = angular.module('myApp', [])
.controller('DataController', ['$scope', 'dataService', function ($scope, dataService) {
$scope.data = dataService.fetchData();
}])
.service('dataService', ['$http', function($http) {
this.fetchData = function() {
$http.get('assets/scripts/data/products.json')
.then(function(response) {
return response.data;
});
};
}]);
HTML
<div class="content">
<ul>
<li ng-repeat="item in data.products">{{item.name}}</li>
</ul>
</div>
I know that $http
, $q
, and $resource
all deal with promises, but it seems like there may be something missing in my implementation.