Currently, I am going through the Angular.js tutorial on pluralsight.com and it has been a smooth experience so far. The tutorials are well-explained and easy to understand. However, I have encountered an issue that I am struggling to resolve on my own. I want my deferred promises to react differently based on whether they succeed or fail, but they always seem to be firing as if they succeeded.
Service:
var resource = $resource('/data/event/:id', {id: '@id'});
return {
getEvent: function (id) {
var deferred = $q.defer();
resource.get({id: id},
function (event) {
console.log("This is (EVENT): " + event);
deferred.resolve(event);
},
function (response) {
console.log("This is (RESPONSE): " + event);
deferred.reject(response);
});
return deferred.promise;
Controller:
$scope.event = eventData.getEvent(2)
.then (
function(event) {
$scope.event = event;
console.log(event);
},
function(response) {
console.log(response);
}
);
In essence, when I input an incorrect id (to load a non-existing JSON file), I want to be alerted about it.