I am currently working on a project that involves using AngularJS with Firebase. In order to retrieve data from Firebase, I have implemented the following code snippets.
Controller.js
$scope.load_msg=function(){
$scope.msg=[];
Chat.load_msg($scope.name,$scope.user.profile.name).then(function(arr){
$scope.msg=arr;
console.log($scope.msg);
},function(error){
console.log(error);
});
}
Service.js
load_msg:function(name,frm){
var deferred = $q.defer();
var sref=ref.child('/Chats/'+frm);
var rref=ref.child('Chats/'+name);
var msgArray=[];
ref.orderByChild("to").equalTo(name).once('child_added',function(messages){
msgArray=msgArray.concat(messages.val());
deferred.resolve(msgArray);
},function(error){
deferred.reject(error);
});
return deferred.promise;
}
Although the msgArray
successfully retrieves all elements queried by on(), only the first object is stored in arr
in the controller. How can I pass the entire array msgArray
through the promise?