I am learning Angular JS $http and $q services from Pluralsight Angular JS tutorial. I mostly created the eventData service that retrieves a JSON from the server using promise q. But when I assigned the promise to the $scope.event, it was just and empty object. Why didn't this method work?
Then I googled to find why it was not working. And got a different code which calls the then() function on the promise and assigns the result to $scope.event
The promise queue is used to avoid passing callbacks. But here we are passing a call back to the then function. And isn't that against the point of having a promise queue in the first place?
// Event Data Function
eventsApp.factory("eventData", function($http, $q){
return {
events : function() {
var deferred = $q.defer();
$http({
method: "GET", url:"http://localhost/data/events/events.php"
})
.success(function(data,status,headers,config){
deferred.resolve(data);
})
.error(function(data,status,headers,config){
deferred.reject(status);
});
return deferred.promise;
}
}
});
// Event controller
var eventController = eventsApp.controller('eventController',function($scope,eventData)
{
$scope.event = eventData.events()
});
// Event Controller from googled code
var eventController = eventsApp.controller('eventController',function($scope,eventData)
{
eventData.events().then(function(result){
$scope.event = result
});
});