AngularJS Resource not Resolved


I am new to AngularJS, so please be gentle.

I want to retrieve data from my backend using the $resource property of AngularJS, however it seems that the property is getting loaded before the actual call is completed.

Currently I have the following code:


MyApp.controller('IndexCtrl', function IndexCtrl($scope, socket, carsRes) {
$ =;


.factory('carsRes', function($resource) {
var result = {}; = $resource('/cars/:id', {}, {
'get': {method: 'GET'},
'save': {method: 'POST'},
'query': {method: 'GET', isArray: true},
'remove': {method: 'DELETE'},
'delete': {method: 'DELETE'}

return result;

But at the point where I want to store in $ the call isn’t completed yet, and a console.log tells me that the $resolved is still false.

How can I wait for the call to be resolved? I’ve read something about $q but it isn’t really clear to me.

Problem courtesy of: DirkZz


This is happening because resource.get() is asynchronous. It returns an empty reference immediately, and is then populated with the actual data once the ajax call is complete.

The best way to handle the result is with a callback function:{ 
// We now have a completed ajax call, with the response
$ = response;

See more examples in the $resource documentation.

Solution courtesy of: jszobody

View additional discussion.