Note that a $resource
returns you an object immediately, before the data is retrieved from the server. This object has a property called $promise
. When the response is finally received from the server, the $promise
gets resolved and the object will now contain the data.
In your case the console.log()
output prints what it does because you're accessing the object immediately, before the server has had a chance to respond.
You can do several things with the object returned by $resource
:
1) Use it in your views in Angular expressions. When the $promise
gets resolved automatically the view will be updated with no effort on your part. For example:
controller:
$scope.users = user_resources.getAll();
HTML:
<ul>
<li ng-repeat="user in users">{{user.name}}</li>
</ul>
2) Use the then()
function as suggested by @doodeec above, but with the correct syntax:
user_resources.getAll().$promise.then(function(data) {
// do something with data
});