I am trying to access a service and then return the response to a Controller for shared data usage. Here is my service which returns the phone List:
app.service('DataService', ['$http', function($http){
this.getData = function(){
var promise = $http.get('js/products.json').then(function(response){
return response.data;
});
return promise;
}
}]);
And in the controller:
app.directive('phoneList',[function(){
return {
restrict: 'E',
templateUrl: 'phone-list.html',
controller: ['$http', '$scope', 'DataService', function($http, $scope, DataService){
DataService.getData()
.then(function(response){
$scope.products = response;
});
console.log($scope.products)
}],
controllerAs: 'homeCtrl'
};
}]);
But when I am logging $scope.products in Console, it just prints undefined. Any leads would be appreciated.
DataServicemust be apromise. So tryDataService.getData().then(function(response) { $scope.products = response });.then()function.