I had some problems with $q.defer();
When I used callbacks instead, my code was working(the view was updated), but with $q.defer();
it's not.
This is my code:
The service:
eventsApp.factory('eventData', function($http, $q) {
return {
getEvent: function(callback) {
var deferred = $q.defer();
$http({method: 'GET', url: '/node/nodejsserver/server.js'}).
success(function(data, status, headers, config){
//callback(data.event);
deferred.resolve(data.event);
console.log('status: ', status, ' data: ', data);
}).
error(function(data, status, headers, config){
deferred.reject(status);
console.log('status: ', status);
});
return deferred.promise;
}
};
});
The controller:
eventsApp.controller('EventController',
function EventController($scope, eventData) {
$scope.event = eventData.getEvent();
}
);
But it doesn't work.
Then I found this answer and I updated my controller like this:
eventsApp.controller('EventController',
function EventController($scope, eventData) {
eventData.getEvent().then(function(result) {
$scope.event = result;
});
}
);
and it works.
What is the difference between the nonworking and the working code?