Message.query({ type:'inbox' });
is an async request.
It will return a reference object, which is populated after the query is finished. From the docs:
It is important to realize that invoking a $resource object method immediately returns an empty reference (object or array depending on isArray). Once the data is returned from the server the existing reference is populated with the actual data.
Handily, you can access the promise which is resolved when the query finishes using the $promise
property.
So try changing your code inside the timeout function to the following:
var messages = Message.query({ type:'inbox' });
// Populate the scope only after the query has resolved.
messages.$promise.then(function(){
$scope.inbox = messages;
});
poll();